0

I am using the js_sys crate to run some Rust code in the browser.

Having difficulty to understand how to use Intl::DateTimeFormat.

All I want is to get the local timezone, which is done with this JS code:

const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;

This does not seem to work in Rust:

let tz = Intl::DateTimeFormat().resolved_options().time_zone();

  --> src/lib.rs:14:14
   |
14 |     let tz = Intl::DateTimeFormat().resolved_options().time_zone();
   |              ^^^^^^^^^^^^^^^^^^^^ did you mean `Intl::DateTimeFormat { /* fields */ }`?

It seems to want me to create a new DateTimeFormat struct, but I just want to get the "default" one, if there's such thing...

How can I get this to work?

Renato
  • 12,940
  • 3
  • 54
  • 85

1 Answers1

0

Thanks to @pauan, and as suggested by @SOFe in the comments, here's how to do it:

let options = Intl::DateTimeFormat::new(&Array::new(), &Object::new())
        .resolved_options();

let tz = Reflect::get(&options, &JsValue::from("timeZone"))
        .expect("Cannot get timeZone")
        .as_string()
        .expect("timeZone is not a String");
Renato
  • 12,940
  • 3
  • 54
  • 85