1

So i have the following string "2013-12-10 23:33:05"

when i do var d = new Date("2013-12-10 23:33:05"); in jsfiddle. http://jsfiddle.net/38TuV/1/

d contains the right object and has no issue.

however in my sites when i do

var date = '2013-12-10 23:33:05';
var d =  new Date(date);

d is Invalid Data. whats even weird if you'll look in the fiddle, i also made that same copy as the second example and it seem to work

This is weird, its probably something specific to my page however i am logging data and it prints '2013-12-10 23:33:05' so i dont know ... Any ideas?

Neta Meta
  • 4,001
  • 9
  • 42
  • 67

3 Answers3

1

If you are sure date will be in the format 'YYYY-MM-DD HH:MM:SS', try this on firefox

var date = '2013-12-10 23:33:05'.replace(' ', 'T');
var d =  new Date(date);
SajithNair
  • 3,867
  • 1
  • 17
  • 23
  • See also: http://stackoverflow.com/questions/3257460/new-date-is-working-in-chrome-but-not-firefox – Jason Aller Mar 19 '14 at 04:14
  • No, don't do that. Parse date strings manually, there are too many issues to leave it up to Date.parse. – RobG Mar 19 '14 at 05:01
  • Try the above in Firefox and Chrome, you may find one treats it as UTC and one doesn't. Happy days. – RobG Mar 19 '14 at 05:17
1

Before ES5, parsing of date strings by Date.parse (which is the same as parsing using the Date constructor) was entirely implementation dependent. ES5 introduced a version of ISO 8601 for compliant implementations, however not all browsers in use are compliant.

Therefore, the best way to parse a string date and time value is to parse it yourself. With the ES5 version of Date.parse, an ISO 8601 format string with no time zone will be treated as UTC, so:

function parseUTCDateTime(s) {
  s = s.split(/\D+/g);
  return new Date(Date.UTC(s[0], --s[1], s[2], s[3], s[4], s[5], 0));
}

parseUTCDateTime('2013-12-10 23:33:05'); // Wed 11 Dec 07:33:05 UTC+0800 2013
RobG
  • 142,382
  • 31
  • 172
  • 209
0

You have a dateclass.js defined in JSFiddle external resources.

I removed it and it works fine. http://jsfiddle.net/38TuV/2/

I don't know why you have it, perhaps it is a shim. Anyway, it also works fine without it in the console.

Using Chromium console (control-shift-J), it worked fine:

var date = '2013-12-10 23:33:05';
var d =  new Date(date);
undefined // chromium console is printing the return value of the assignment, undef is OK
date
"2013-12-10 23:33:05"
d
Tue Dec 10 2013 23:33:05 GMT-0500 (EST)
Paul
  • 26,170
  • 12
  • 85
  • 119
  • yea, in the fiddle it work just fine for both the examples .. in my site however it does. i am starting to think its a plugin or something that change how Date works . – Neta Meta Mar 19 '14 at 03:54
  • I have the dataclass.js because it helps with nice formatting and weight almost nothing. The issue is with firefox. chrome seems to be able to parse the above string while FF doesnt. – Neta Meta Mar 19 '14 at 04:10