来自时间戳的错误日期 [英] Wrong date from timestamp
问题描述
我遇到了一个我自己无法解决的问题。
I have a problem which can't solve by myself.
这是从我的网站数据库中获取的时间戳1308085200。它代表2011-06-15 12:00:00
Here is timestamp 1308085200 taken from my website database. It represents 2011-06-15 12:00:00
这是我用来以人类可读格式获取日期的javascript代码。
Here is javascript code which I use to "fetch" date in human readable format.
var date = new Date(1308085200 * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getUTCHours();
var minute = date.getUTCMinutes();
var seconds = date.getUTCSeconds();
问题是我的结果错了。例如上面的代码显示2011-5-15 21:0:0而不是2011-06-15 12:00:00
The problem is that I get wrong results. For example the code above shows 2011-5-15 21:0:0 instead of 2011-06-15 12:00:00
我做错了什么以及如何修复了吗?
What I do wrong and how to fix that ?
推荐答案
JavaScript的 Date :: getMonth()
返回一个从0到11的从零开始的整数,这就是为什么你的日期显示的是5月而不是6月。
JavaScript's Date::getMonth()
returns a zero-based integer from 0 to 11 which is why your date is showing May instead of June.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getMonth
至于时间部分,这是我得到的(AEST)
As for the time portion, this is what I get (AEST)
var d = new Date(1308085200000); // Wed Jun 15 07:00:00 GMT+1000 (EST)
d.toUTCString() // Tue, 14 Jun 2011 21:00:00 GMT
d.getUTCFullYear() // 2011
d.getUTCMonth() // 5
d.getUTCDate() // 14
d.getUTCHours() // 21
d.getUTCMinutes() // 0
d.getUTCSeconds() // 0
看起来你的时间戳实际上并不是你的想象是。
Looks like your timestamp is actually not what you think it is.
这篇关于来自时间戳的错误日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!