-1

I have to display dates received from the service in UI using javascript.

 var now = new Date ('2015-09-26T09:52:19');
 alert(now) // it alerts Sat Sep 26 2015 02:52:19 GMT-0700 (Pacific Daylight Time)

I am in PST timeZone, I am wondering why Javascript treats date received as UTC.

One more observation is if i remove "T" from date. Javascript doesn't treat it as UTC date.

manthan davda
  • 319
  • 1
  • 3
  • 13

1 Answers1

1

"Javascript does not have native support for timezones. When you create a date in Javascript from an ISO string that is in the site's timezone, the Date object will automatically be converted into the browser's local timezone (which may be different than the site's timezone). Strings with no timezone specified are also created in the browser's local timezone." - Jason Yuen, Aug 25 2013, Dealing with Timezones in Javascript Nulogy.com

Have a look into momentjs (www.momentjs.com) if you wish to convert easily between timezones.

This is taken directly from their website.

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");  
var losAngeles = newYork.clone().tz("America/Los_Angeles");  
var london     = newYork.clone().tz("Europe/London");  

newYork.format();    // 2014-06-01T12:00:00-04:00  
losAngeles.format(); // 2014-06-01T09:00:00-07:00  
london.format();     // 2014-06-01T17:00:00+01:00  
Adrian David Smith
  • 574
  • 1
  • 4
  • 26
  • One more observation - Only for chrome its getting converted to local time zone for other browsers date is treated as local time zone date.. – manthan davda Oct 08 '15 at 15:08