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?


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.


This is a fairly old question, but for those who may stumble upon this in a search, it's highly suggested that you switch to the currently maintained version of Date.js (https://github.com/abritinthebay/datejs/) if you're encountering unexpected behavior.

The versions on the Google code base and datejs.com both have unfixed bugs that are unlikely to ever be addressed, as the original developer seems to have abandoned the project.

链接地址: http://www.djcxy.com/p/18636.html

上一篇: Javascript日期+5不会增加月份

下一篇: 使用date.js在中午和午夜之间产生混乱