Thorsten Thorsten - 7 months ago 18
Javascript Question

Which datetime format to get from server and pass to browser to format in Javascript?

I save any datetimes in MS SQLServer 2012 as datetime in UTC.
I would like to get via coldfusion on the backend the datetime and pass it to the browser in order to format it in the browser via JavaScript and format it to the locale of the user get via the browser.

What datetime format should I use to pass the datetime object from the backend server to the browser?

Answer

What datetime format should I use to pass the datetime object from the backend server to the browser?

You have at least two solid options:

  1. Send it as milliseconds-since-The-Epoch or seconds-since-The-Epoch. This is the number of milliseconds (or seconds) that has passed since January 1st 1970 at midnight UTC. In JavaScript, you get a Date instance from a number of milliseconds since The Epoch via new Date(num).

    Example: The number 1461139200000 unambiguously identifies April 20th 2016 at 8 a.m. exactly UTC.

  2. Send it as a full ISO-8601 date/time using UTC. This is supported as an input format to the JavaScript Date constructor on all modern browsers, as it was added to the JavaScript specification in 2009. (Details here, note that what was added was a subset of the full ISO-8601, hence my saying use a "full" date/time.)

    Example: The string 2016-04-20T08:00:00.000Z unambiguously identifies April 20th 2016 at 8 a.m. exactly UTC.

The advantage to the second form is that it's easier for you to debug when looking at HTTP responses and such, as it's in a nice simple human-readable form.