user850234
user850234

Reputation: 3463

How to get the current local time using the system time zone and UTC time from the server in javascript

I have got the UTC time as Sat, 19 May 2012 11:26:51 +0000 in the json response and have the system time zone as +0530. How can i convert the UTC time to the local time using the two available result? I want the date as isoDate format and time as isoTime format. Please help how to do that?

Upvotes: 1

Views: 2374

Answers (1)

KooiInc
KooiInc

Reputation: 123016

Convert the JSON datetime to a Date Object (use the string without the +0000 timezone information) and add/subtract the client side timezoneOffset:

var received = new Date('Sat, 19 May 2012 11:26:51'),
    clientDate = new Date(new Date().getTimezoneOffset()*-60000 
                  + received.getTime());
//note: -60000 reverses the sign of the timezone offset 
//      clientDate is calculated in milliseconds.
//alternatively you can set [received] directly to the local datetime using:
received.setMinutes(received.getMinutes()+(-(new Date().getTimezoneOffset()));

In my timezone (GMT+2) clientDate now reads Sat May 19 2012 13:26:51 GMT+0200

Upvotes: 1

Related Questions