Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is direct comparison of Javascript ISO Date Strings safe?

Is it safe to directly compare ISO Date Strings like this:

"2018-03-16T18:00:00.000z" > "2018-04-16T18:00:00.000z" // false

It seems as long as leading zeros are used (proper ISO formatting) this comparison is safe and there is no need to convert the values to Date Objects. Am I overlooking something?

like image 316
Slbox Avatar asked Mar 16 '18 18:03

Slbox


1 Answers

With the given format of a ISO 8601 time,

2018-03-16T18:00:00.000Z
                       ^

you could use a direct string comparison, because the given time zone is a

Coordinated Universal Time (UTC)

If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".

like image 91
Nina Scholz Avatar answered Oct 31 '22 11:10

Nina Scholz