'How to import DateTimeFormatOptions in

I want to store a DateTimeFormatOptions for date.toLocaleString() to use in multiple places in my app. I defined it like:

export const timeFormat = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}

And I get:

Argument of type '{ month: string; day: string; hour: string; minute: string; hour12: boolean; timeZoneName: string; timeZone: string; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Types of property 'month' are incompatible.
    Type 'string' is not assignable to type '"numeric" | "2-digit" | "short" | "long" | "narrow" | undefined'.

But I can't figure out import DateTimeFormatOptions. Eventually I just wrote a helper method that formats the date, but I still may need to import it because I may allow the user to change date preferences.



Solution 1:[1]

It's in the Intl object. You won't need to import it. Just set the type to Intl.DateTimeFormatOptions.

const timeFormat: Intl.DateTimeFormatOptions = { month: 'numeric', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZoneName: 'short', time  Zone: 'UTC'}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominic Roy-Stang