Reputation: 9583
I have the following ISO dates:
(UTC) 2013-10-17T05:23:34.387
(PST) 2013-10-17T05:23:34.387-08:00
I would like to display the date in PST. (The -08:00 is the offset from UTC to PST)
When I use:
alert(new Date('2013-10-17T05:23:34.387'))
alert(new Date('2013-10-17T05:23:34.387-08:00'))
I get:
Thu Oct 17 2013 06:23:34 GMT +0100 (GMT Summer Time)
Thu Oct 17 2013 14:23:34 GMT +0100 (GMT Summer Time)
The ISO date with no offset is from the following C# (edited appropriately for this question):
item.CreatedDate = DateTime.Now.ToUniversalTime();
/////
var pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
date = TimeZoneInfo.ConvertTimeFromUtc(item.CreatedDate, pst),
var json = JsonConvert.SerializeObject(date, Formatting.Indented);
The ISO date with an offset is from the following C# (edited appropriately for this question):
item.CreatedDate = DateTime.Now.ToUniversalTime();
/////
var pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var offset = pst.BaseUtcOffset;
date = new DateTimeOffset(TimeZoneInfo.ConvertTimeFromUtc(item.CreatedDate, pst), offset);
var json = JsonConvert.SerializeObject(date, Formatting.Indented);
My question is, how to I maintain the time zone information in the JSON and display the date as PST in the browser?
Upvotes: 3
Views: 3978
Reputation: 7475
From MSN about Date.parse
in javascript:
The local time zone is used to interpret arguments that do not contain time zone information.
Update: You can keep time zone using Json.NET serialization settings:
var json = JsonConvert.SerializeObject(date,
Formatting.Indented,
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Local
});
Update2:
About display in javascript.
Yes it maintains the offset, but when it converts date to string it uses local timezone.
Try the following methods:
alert(new Date('2013-10-17T05:23:34.387-08:00').toGMTString())
alert(new Date('2013-10-17T05:23:34.387-08:00').toUTCString())
Upvotes: 3