Search code examples
javascripttypescriptmicrosoft-edge

24 hrs format is not returning correct value in MS-edge


const gmtDate = new Date("2023-11-03T18:49:27.652173+00:00"); // Date in GMT

const localTimeZone = "Asia/Kolkata"; // Replace with your desired time zone

const options = {
  weekday: 'short',
  year: 'numeric',
  day: 'numeric',
  month: 'short',
  hour: '2-digit',
  minute: '2-digit',
  hour12: false,
  timeZoneName: 'short',
  timeZone: localTimeZone,

};

const dateFormatter = new Intl.DateTimeFormat(navigator.language, options);
const localDateStr = dateFormatter.format(gmtDate);

console.log(localDateStr);

Current output -> "Sat, 4 Nov, 2023, 24:19 IST"

Expected output -> "Sat, 4 Nov, 2023, 00:19 IST"

Objective -> I was trying to get a 24hrs local time from 24hrs format UTC time.

TS link -> TS example

Details Browser -> MS-edge

enter image description here

Can somebody please help me out !

It is working everywhere else except MS-edge


Solution

  • Don't use the hour12 options, instead use hourCycle option. This will fix the bug on all browsers.

    const gmtDate = new Date("2023-11-03T18:49:27.652173+00:00"); // Date in GMT
    const localTimeZone = "Asia/Kolkata"; // Replace with your desired time zone
    
    const options = {
      weekday: 'short',
      year: 'numeric',
      day: 'numeric',
      month: 'short',
      hour: '2-digit',
      minute: '2-digit',
      hourCycle: 'h23',
      timeZoneName: 'short',
      timeZone: localTimeZone,
    };
    
    const dateFormatter = new Intl.DateTimeFormat(navigator.language, options);
    const localDateStr = dateFormatter.format(gmtDate);
    
    console.log(localDateStr);