Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between datetime in ISO 8601 and UTC formats in javascript?

I pick some date and time in javascript and then want to store it on server (.NET). Dates are supposed to be in future from the current moment (so they won't be before 1970). Having read topics here on SO I learnt it's better to store date as a string and people suggest using Date.prototype.toISOString() or Date.prototype.toUTCString(). I've read that toISOString() is not available in IE 7. And I'd like to know other differences, when I should choose one or another function.

like image 560
myroman Avatar asked Jan 23 '14 06:01

myroman


People also ask

Is ISO 8601 always UTC?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What is UTC datetime format?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

How do I convert ISO date to UTC?

Use the getTime() method to convert an ISO date to a timestamp, e.g. new Date(isoStr). getTime() . The getTime method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.

Is Javascript date always UTC?

The timezone is always zero UTC offset, as denoted by the suffix " Z ". For Timezone work, moment. js and moment.


1 Answers

They're for different purposes.

  • UTC is the primary time standard by which the world regulates clocks and time.
  • ISO is standard format time. ISO also supports ms in its format.

So if you want to send data to the server, send the ISO, because ISO is the standard format:

var date = new Date(); sendDate(date.toISOString()); 

You can also use toISOString in IE7 polyfill.

like image 58
Pinal Avatar answered Sep 21 '22 12:09

Pinal