4

The following code outputs outputs NaN in Chrome while Firefox generates 1247547600000.

var str = "2009/07/13 24:00:00-0500";
document.write(Date.parse(str));

See this fiddle.

How to solve this? Thank you!

EDIT: I've got the data from another company. I cannot change the data it generates. So What are the suggestions here?

The data generated:

{"day":"2009-07-13", "work":["11:16:35-12:03:12", "12:32:48-13:26:28", "13:39:09-13:39:12", "13:41:03-13:41:05", "14:18:09-24:00:00"]},
{"day":"2009-07-14", "work":["00:00:00-07:22:25", "07:22:25-07:22:28", "10:10:04-10:10:31", "10:10:32-10:15:33", "10:18:07-10:21:19", "11:04:49-11:06:15", "11:12:50-11:19:05", "11:19:11-11:19:19", "11:45:50-11:51:42", "11:51:43-11:53:55", "14:03:13-14:13:04", "14:23:55-14:31:28", "14:31:28-14:38:00", "14:38:00-14:49:04", "16:34:56-16:44:33", "16:46:37-16:48:10", "16:48:11-24:00:00"]}
sozhen
  • 7,627
  • 14
  • 36
  • 53

3 Answers3

7

Just had this problem and researched it.

According to Wikipedia: (see http://en.wikipedia.org/wiki/ISO_8601 )

Midnight is a special case and can be referred to as both "00:00" and "24:00". The notation "00:00" is used at the beginning of a calendar day and is the more frequently used. At the end of a day use "24:00". Note that "2007-04-05T24:00" is the same instant as "2007-04-06T00:00" (see Combined date and time representations below).

So it seems Chrome is wrong.

kwaclawek
  • 91
  • 2
  • 5
5

You are asking the browser to parse an invalid time. 24:00 isn't valid. You probably mean 0:00 of the next day. Chrome is correct in rejecting it. Firefox is simply more forgiving.

Consider that there are 24 hours in the day. If the first hour is 00, then the last hour is 23.

Greg
  • 23,155
  • 11
  • 57
  • 79
  • I actually got data from another company and if we cannot change the data it generates, what's your suggestions? – sozhen Jul 19 '12 at 15:36
  • 2
    talk to the company about fixing their bad data, barring that use a regex to convert 24:00:00 to 00:00:00, but that only fixes part of the problem, did the comany mean for it to be the next day? If so then you also have to increment the day, if not... then that data is really confusing – JaredMcAteer Jul 19 '12 at 15:37
  • I put the data in my question.The did not mean 24:00:00 is increment of the day. Only 00:00:00 is the next day. – sozhen Jul 19 '12 at 15:48
1

Chrome is right, 24:00 doesn't exists. The day ends at 23:59:59, and then the next day starts on 00:00:00. Your code should not generate dates with 24:00:00, 'cause you have no guarantee of how it is interpreted by the browser or even if it works, now and in the future.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Marcelo De Zen
  • 9,439
  • 3
  • 37
  • 50
  • The data we've got is from another company and we need to generate chart from that data. So when I try to parse the data, then the error comes up. – sozhen Jul 19 '12 at 15:35
  • So you have a problem my friend. I think that the only solution you have is to check the date string and change it where you found 24:00... – Marcelo De Zen Jul 19 '12 at 15:40
  • Thanks, but would that be a difference with respect to the unix timestamp? So if I manually changed their data from 24:00:00 to 23:59:59, then I need to add the unix timestamp for one second to make all my data showing correctly? Is that right? – sozhen Jul 19 '12 at 15:43
  • I think it depends on what exactly is 24:00:00 for the company. I'm not sure if the date 07/13/2009 24:00:00 means 07/13/2009 00:00 or 07/14/2009 00:00. I think your suggestion is correct, but it's better if you can check with the company what it means to them 24:00. If it means 00:00 of the next day, then you can add 1 milisecond to the 23:59 of the same day on the string. – Marcelo De Zen Jul 19 '12 at 16:08