Search code examples
javascriptdatedatejs

Confusion over noon versus midnight with date.js


I am using date.js in my project, and I am encountering something I didn't expect with respect to dates that had time components of exactly noon or exactly midnight.

When I use the native javascript Date.parse method, I get the result I expected when parsing a date:

// plain old javascript
var date = new Date(Date.parse("10/21/2010 12:00:00 PM"));
alert(date);

I get this:

Thu Oct 21 2010 12:00:00 GMT-0500 (Central Daylight Time)

I interpret this as noon on October 21, 2010, which is what I think my input date is. However, when I import date.js and try something similar,

// using date.js
var date = Date.parseExact("10/21/2010 12:00:00 PM", "M/d/yyyy hh:mm:ss tt");
alert(date);

I get this:

Fri Oct 22 2010 00:00:00 GMT-0500 (Central Daylight Time)

This looks to me like midnight on the following day. I have no idea why it's interpreting this as midnight, or at any rate why it's interpreting this differently than the default Date.parse in javascript. Am I confused over the meaning of 12:00 PM? Am I misusing date.js's parseExact?


Solution

  • Looks like a version issue:

    http://jsfiddle.net/4QK8Q/
    http://jsfiddle.net/JKg3n/

    The first one is your file (from the google code base), the second is from their website (datejs.com). Funnily enough they look like the same version, but obviously aren't. I'm not familiar enough with the library to tell what's going on, but I suggest filing a bug report.