Home > Software engineering >  Convert to another time zone javascript
Convert to another time zone javascript

Time:04-24

I have api which will give a date and timezone

{"time":"2021-01-01 10:10:10","zone":"America/Denver"}

we are not sure which timezone they are providing

How to 1)convert time to local timezone 2)convert time to another timezone

i tried

date.toLocaleString("en-US", {timeZone: "any timezone"});

but this will consider date as your local timezone only

CodePudding user response:

you can use moment js for converting zone

var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");

jun.tz('America/Los_Angeles').format('ha z');  // 5am PDT
dec.tz('America/Los_Angeles').format('ha z');  // 4am PST

jun.tz('America/New_York').format('ha z');     // 8am EDT
dec.tz('America/New_York').format('ha z');     // 7am EST

jun.tz('Asia/Tokyo').format('ha z');           // 9pm JST
dec.tz('Asia/Tokyo').format('ha z');           // 9pm JST

jun.tz('Australia/Sydney').format('ha z');     // 10pm EST
dec.tz('Australia/Sydney').format('ha z');     // 11pm EST

CodePudding user response:

Use this code.

date_default_timezone_set('Asia/Colombo');

In my case, I have used 'Asia/Colombo', you can use your related timezone

  • Related