Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date.parse bug when dash-delimited and starts with year

Am seeking confirmation if this is a bona fide documentation and/or implementation bug with Javascript's Date.parse method.

The docs I'm referring to are at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse and they say 'If you do not specify a time zone, the local time zone is assumed.'

But the following code shows that, despite not specifying a time zone, local time is not being assumed (but rather my timezone offset is being applied), if the string passed to Date.parse begins with the 4-digit year representation, and is dash-delimited.

var euroStyleDate = '2011-10-04';
var amerStyleDate = '10/04/2011';
var euroStyleParsed = Date.parse(euroStyleDate);
var amerStyleParsed = Date.parse(amerStyleDate);

console.log(euroStyleParsed); //1317686400000
console.log(amerStyleParsed); //1317700800000

console.log(new Date(euroStyleParsed)); 
//Date {Mon Oct 03 2011 20:00:00 GMT-0400 (Eastern Daylight Time)}
console.log(new Date(amerStyleParsed)); 
//Date {Tue Oct 04 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}

There may even be other cases, and I'm sure I'm not the first to discover this if I am incorrect. So beyond confirmation, I'd surely love to be pointed at more in-depth information on this if anybody knows of pertinent links.

I'm experiencing this in FF3, Chrome for Windows and of course just to be special IE8 doesn't even seem to able to perform the conversion on 2011-10-04 whatsoever: I'm just getting an empty string in my application

Thanks in advance for any further insight or resources.

like image 967
Dexygen Avatar asked Oct 05 '11 16:10

Dexygen


People also ask

What can I use instead of date parse?

If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead. The DATE function converts a number, string or date expression to a date type.

How does date parse work?

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

What does parsing date mean?

Definition and Usage parse() parses a date string and returns the time difference since January 1, 1970. parse() returns the time difference in milliseconds.


1 Answers

I ran into this concept, too. For anyone googling "Javascript dates dashes slashes" like I was, this is the clearest demonstration that I can think of as to what's going on here.

In short, slashes means local time zone, and dashes means UTC. Other answers has explanations regarding why.

<script type="text/javascript">

var 
testB = new Date("2012/02/09"),
testC = new Date("2012-02-09");


alert(testB.toString());
alert(testC.toString());
alert(testC.toUTCString());
</script>
like image 153
Narpas Avatar answered Nov 02 '22 21:11

Narpas