-3

I'm trying to convert UTC time 7:27:02 AM to the local timezone. Converting just HH:MM:SS AM. Currently, I'm in GMT05:30.

Pratap Sharma
  • 2,633
  • 2
  • 18
  • 32
  • 1
    Really it is just one google search away... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC – Akxe Jul 04 '19 at 12:12

1 Answers1

-2

Here, first, on lines 1 and 2, you get the current date and the current time zone offset, which is the difference in minutes (which you have to convert to milliseconds) between the UTC time and the local time. Then you'll get the timestamp from the date that you want, which is 7:27:02 AM (for PM you'll have to sum 12 hours), and subtract the offset. Then you just have to convert the timestamp to Date Object.

var date = new Date();
var offset=new Date().getTimezoneOffset()*60000;
var date2 = new Date(date.getFullYear(),date.getMonth(),date.getDay(),7,27,2).getTime()-offset
date2=new Date(date2)
console.log(date2)
  • Sorry, but this is an anti-pattern. Adding it subtracting an offset from the timestamp does not adjust for time zone. It produces an entirely different moment in time. – Matt Johnson-Pint Jul 04 '19 at 16:16