1

I have a ASP.NET webapi with a DateTimeZoneHandling set to Local and I am getting two different results when converting to a JavaScript date.

Example 1

Date returned from server 1932-10-13T00:00:00-04:00

var strDate = new Date("1932-10-13T00:00:00-04:00");

strDate.toISOString();    // 1932-10-13T04:00:00.000Z
strDate.toLocaleString(); // 10/12/1932, 11:00:00 PM

Example 2

Date returned from server 2013-05-09T00:00:00-04:00

var strDate = new Date("2013-05-09T00:00:00-04:00");

strDate.toISOString();    // 2013-05-09T04:00:00.000Z
strDate.toLocaleString(); // 5/9/2013, 12:00:00 AM

I expected behaviour should always be midnight as the dates returned from the server are always midnight. It appears all recent dates parse correctly, however, dates far in the past are incorrect.

4
  • They are midnight at UTC-4 timezone. The toISOString returns that date and time as a UTC/Zulu time (z at the end) and toLocaleString tells you the date and time in your time zone. Commented Nov 23, 2018 at 15:53
  • @HMR Why is it that 1932-10-13T00:00:00-04:00 when converted to local time is at 10/12/1932, 11:00:00 PM and 2013-05-09T00:00:00-04:00 converts 5/9/2013, 12:00:00 AM though? thats the part i don't understand. My expectation is that they will both be midnight on the same day, not 11pm for the previous day Commented Nov 23, 2018 at 16:06
  • Timezones and DST have changed a couple of times over time. Commented Nov 23, 2018 at 16:09
  • @HMR Is there any way to account for these changes? Commented Nov 23, 2018 at 16:13

2 Answers 2

1

The timezone can vary in some locales, for example, I'm UTC-0300, and on certain season shifts it becomes UTC-0200, so it indicates that your locale changed the offset too, making it display the time one hour lesser, basicaly because you locale adopted a different offset along the year.

The example bellow, I've changed your first example to use the same day and month than the second one, so that it proves you that old dates has nothing to do with it.

console.log("Example One");
var strDate = new Date("1932-05-09T00:00:00-04:00");

console.log(strDate.toISOString());
console.log(strDate.toLocaleString());

console.log("--------------------------");
console.log("Example Two");

var strDate2 = new Date("2013-05-09T00:00:00-04:00");

console.log(strDate2.toISOString());
console.log(strDate2.toLocaleString());


Further explanation on UTC/Zulu time

It has normalized the iso date to a zulu date (zero offset iso date). It is still the same datetime, but it has converted the timezone offset into hours making the timezone offset zero.

date      [2013-05-09]
separator [T]
time      [00:00:00]
offset    [-04:00]

The fundamental aspect is that 00:00:00.000-04:00 is the same than 04:00:00.000Z.

Sign up to request clarification or add additional context in comments.

2 Comments

But did you notice that in the first example the toLocaleString() converted to 11PM instead of 12AM? Unless the time zone in the first example was UTC-0500 that doesn't make much sense =S
@ThiagoLoddi It's normal, your locale timezone offset changes because your country/state might adopt different timezones along the year, it causes confusion some times. I've added an explanation for that on my answer.
0

If you're simply trying to display the date as someone living in that time would have remembered it (in your case, October 13th happened on October 13th), you may be able to (ab)use Moment Timezone, which appears to format the date as expected:

moment.tz("1932-10-13T00:00:00-04:00", "America/Toronto").tz("UTC").format();  // 1932-10-13T04:00:00Z

In your case, this hacktechnique results in 1932-10-13T04:00:00Z which may be what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.