Javascript UTC timestamp for local timezone

I am trying to convert the timestamp returned from a JSON resource to javascript, which is displayed in UTC in the local time zone of users. Below I am trying to tune using custom offset.

Example UTC output for a date: Tue Mar 27 2012 02:29:15 GMT-0400 (EDT)

the code

var date = new Date(data.date_created); //Data.date_created coming from json payload var offset = date.getTimezoneOffset() //Get offset var new_date = new Date(date offset); //Add offset to userdate 

I am afraid using the appropriate method to achieve this. Can someone point me in the right direction?

+4
source share
1 answer

Maybe I missed something, but

 var date = new Date( data.date_created ); 

does what i think you want.

 >>> d=new Date('Tue Mar 27 2012 02:29:15 GMT-0800') Date {Tue Mar 27 2012 06:29:15 GMT-0400 (EDT)} >>> d.toLocaleString() "Tue Mar 27 06:29:15 2012" >>> d=new Date('Tue Mar 27 2012 02:29:15 GMT+0300') Date {Mon Mar 26 2012 19:29:15 GMT-0400 (EDT)} >>> d.toLocaleString() "Mon Mar 26 19:29:15 2012" 

Please note that changing the GMT offset from -8 to +3 changes the resulting time by 11 hours.

+5
source

Source: https://habr.com/ru/post/1403673/


All Articles