Time date option as a separate variable

Viewed 86

I am currently having a bit of a headache trying to format the time to display on a web component with Typescript /React

The codes below work fine:

new Date(myDate)
   .toLocaleTimeString(
   'en-US',
    { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour12: false });

However I am trying to reuse that format option on multiple locations, so I am trying this and it is not working:

const timeformat = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour12: false };

new Date(status.start).toLocaleTimeString('en-US',timeformat);

TypeScript is throwing error as such:

Argument of type '{ weekday: string; year: string; month: string; day: string; hour12: boolean; }' is not assignable to parameter of type 'DateTimeFormatOptions'.
  Types of property 'weekday' are incompatible.
    Type 'string' is not assignable to type '"short" | "long" | "narrow" | undefined'.  TS2345

I have tried

const timeFormat : DateTimeFormatOptions = {...}

But it can't find the type.

Please help!!

Thank you so much!!

1 Answers

This should fix your issue:

let timeformat = new Intl.DateTimeFormatOptions('en-US', { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour12: false });
Related