Skip to content

Missing "options" parameter from Date.to_locale_time_string #4383

Closed
@paul-hansen

Description

@paul-hansen

According to the MDN docs, toLocalTimeString takes an options parameter but it's missing from js-sys
js-sys::Date.to_locale_time_string

Workaround

You can use Reflect to call it directly. Here's a wrapper I made to do this.

use js_sys::{Date, Reflect};
use wasm_bindgen::JsValue;

pub fn to_local_time_string(date: &Date, locale: &str, options: &JsValue) -> Result<String, JsValue> {
    let method = Reflect::get(&date.into(), &JsValue::from("toLocaleTimeString"))?;
    let result = js_sys::Function::from(method).call2(
        &date.clone().into(),
        &JsValue::from(locale),
        options,
    )?;

    // Convert the result to a string and return
    result
        .as_string()
        .ok_or_else(|| JsValue::from("Failed to convert to string"))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions