Home > Blockchain >  Converting date from one timezone to other timezone using ISO date string
Converting date from one timezone to other timezone using ISO date string

Time:10-19

I want to convert a date(10/14/2022 8:04 PM) from America/New_York timezone
to Asia/Tokyo'timezone as shown below.
I have used ISO format with 'Asia/Tokyo' timezone. But I am getting one hour less after conversion
because 08:04:00 p.m. Friday October 14, 2022 in America/New_York
converts to 09:04:00 a.m. Saturday October 15, 2022 in Asia/Tokyo.

Even for other timeZone I have also tried but problem is same.
Please help me to understand the problem in this code

let d = new Date('2022-10-14T20:04:00.000 09:00')

console.log(
  d.toLocaleString('ko-KR', {
    timeZone:'Asia/Tokyo', 
    hour12:false
  })
);

Result is: 2022. 10. 14. 20시 4분 0초

CodePudding user response:

New York time zone is -4 hours (not 9hours) as yours.
see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

In your case of -- 10/14/2022 8:04 PM at America/New_York:

let dateUTC = new Date('2022-10-14T20:04:00.000-04:00') 

console.log('Verify America/New_York date value: (where timeZone is -4 hours according DST)')
console.log( 'America/New_York -->', dateUTC.toLocaleString('en-US', { timeZone: 'America/New_York', hour12:true }) )


console.log( '\nAsia/Tokyo -->', dateUTC.toLocaleString('ko-KR', { timeZone: 'Asia/Tokyo', hour12:false }) )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

As you can see, there is no conversion to do. Just set the correct date value indicating its original timezone offset (take care of possible daylight saving time [DST]).
Then just display your date/time with the local values you want.

  • Related