date.js 对中午和午夜的混淆
我在项目中使用 date.js,但遇到了一些意想不到的情况对于具有正午或正午夜时间成分的日期。
当我使用本机 javascript Date.parse
方法时,我在解析日期时得到了预期的结果:
// plain old javascript
var date = new Date(Date.parse("10/21/2010 12:00:00 PM"));
alert(date);
我得到以下结果:
2010 年 10 月 21 日星期四 12:00:00 GMT-0500(中部夏令时间)
我将此解释为 2010 年 10 月 21 日中午,这就是我认为我输入的日期。但是,当我导入 date.js 并尝试类似的操作时,
// using date.js
var date = Date.parseExact("10/21/2010 12:00:00 PM", "M/d/yyyy hh:mm:ss tt");
alert(date);
我得到以下结果:
2010 年 10 月 22 日星期五 00:00:00 GMT-0500(中部夏令时间)
在我看来,这就像第二天的午夜。我不知道为什么它会将其解释为午夜,也不知道为什么它的解释与 javascript 中默认的 Date.parse
不同。我是否对 12:00 PM 的含义感到困惑?我是否滥用了 date.js 的 parseExact ?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来像是版本问题:
http://jsfiddle.net/4QK8Q/
http://jsfiddle.net/JKg3n/
第一个是您的文件(来自谷歌代码库),第二个来自他们的网站(datejs.com)。有趣的是,它们看起来像同一个版本,但显然不是。我对这个库不够熟悉,无法判断发生了什么,但我建议提交一份错误报告。
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.
这是一个相当老的问题,但是对于那些可能在搜索中偶然发现这个问题的人,强烈建议您切换到当前维护的 Date.js 版本(https://github.com/abritinthebay/datejs/) 如果您遇到意外行为。
Google 代码库和 datejs.com 上的版本都有未修复的错误,这些错误不太可能得到解决,因为原始开发人员似乎已经放弃了该项目。
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.