开发者

Confusion over noon versus midnight with date.js

开发者 https://www.devze.com 2023-04-05 09:21 出处:网络
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.

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 javasc开发者_运维问答ript
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号