Convert GMT to IST (India Standard Time) using javascript?

Shubham Seth picture Shubham Seth · Jan 15, 2020 · Viewed 10.7k times · Source

I have a date format is GMT or UTC.

var mydate  = '2020-01-14T17:43:37.000Z'

I want to convert this date in IST format so according to this date the output I need in this format.

var date = '2020-Jan-15 12:45'


Answer

Matt Johnson-Pint picture Matt Johnson-Pint · Jan 15, 2020

You can specify an IANA time zone identifier in the options passed to toLocaleString. The identifier for India is Asia/Kolkata.

var s = new Date('2020-01-14T17:43:37.000Z').toLocaleString(undefined, {timeZone: 'Asia/Kolkata'});

This will do the correct time zone conversion, as the input is in UTC (as specified by the Z at the end).

undefined means to use the user's locale for the formatting of the date and time. This is usually what you want. If you want a more particular format (like what you specified in your question), you can provide a specific locale string and/or adjust the other options for toLocaleString, as given in the docs.

Also, note that the conversion in your question is incorrect. India is 5 hours an 30 minutes offset from UTC. Thus the correct output is 2020-01-14 23:13:37 (in whatever format you like)