2

The following code seems to work in Firefox but not in Chrome:

var d = Date.parse("Sep 23, 2012 24:0:0");
alert (d);

It alerts the milliseconds in Firefox but I get a NaN in Chrome.

Of course, the last 3 numbers (24:0:0) might not be counted as a 'date', but I'd like to have a simple solution to this to make the code work cross-browser.

chembrad
  • 887
  • 3
  • 19
  • 33
Flame
  • 6,663
  • 3
  • 33
  • 53
  • Note: Chrome just has an issue with invalid times -- `alert(Date.parse("Sep 23, 2012 23:0:0"))` works fine. You might be better off parsing & using the Date Constructor yourself. – mike Sep 18 '12 at 15:04

2 Answers2

2

This should work for the format you listed:

var parts = "Sep 23, 2012 21:1:2".match(/(.*) (\d+):(\d+):(\d+)/)
var d = new Date(parts[1])
d.setHours(parts[2])
d.setMinutes(parts[3])
var tstamp = d.setSeconds(parts[4])

Not sure if you ultimately want a Date object or integer timestamp, but this should get you both.

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
1

Chrome doesn't seem to support time...

try Date.parse("Sep 23, 2012"); it should work

Date.parse is not supposed to parse time according to specs. You need third party code. See : What is the best way to parse a time into a Date object from user input in Javascript?

Community
  • 1
  • 1
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89