28

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.

yesennes
  • 1,147
  • 1
  • 10
  • 19

1 Answers1

53

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', timeZone: 'UTC' }
Dominic Roy-Stang
  • 1,554
  • 13
  • 16