Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing "options" parameter from Date.to_locale_time_string #4383

Open
paul-hansen opened this issue Dec 26, 2024 · 0 comments · May be fixed by #4384
Open

Missing "options" parameter from Date.to_locale_time_string #4383

paul-hansen opened this issue Dec 26, 2024 · 0 comments · May be fixed by #4384
Labels

Comments

@paul-hansen
Copy link

paul-hansen commented Dec 26, 2024

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"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant