人类可读格式的时间戳 [英] Timestamp to human readable format

查看:151
本文介绍了人类可读格式的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javascript从unix时间戳转换为人工表示时有一个奇怪的问题

Well I have a strange problem while convert from unix timestamp to human representation using javascript

这是时间戳

1301090400

这是我的javascript

This is my javascript

var date = new Date(timestamp * 1000);
var year    = date.getFullYear();
var month   = date.getMonth();
var day     = date.getDay();
var hour    = date.getHours();
var minute  = date.getMinutes();
var seconds = date.getSeconds();  

我预计结果将是2011年2月25日22 00 00.但它是2011年,2,6 ,0,0,0
我想念的是什么?

I expected results to be 2011 2, 25 22 00 00. But it is 2011, 2, 6, 0, 0, 0 What I miss ?

推荐答案

getDay()返回星期几。要获取日期,请使用 date.getDate() getMonth()检索月份,但月份为零,所以使用 getMonth()+ 1 应该给你正确的一个月这里的时间价值似乎没问题,虽然这里的时间是23(GMT + 1)。如果您想要通用值,请将 UTC 添加到方法中(例如 date.getUTCFullYear() date.getUTCHours()

getDay() returns the day of the week. To get the date, use date.getDate(). getMonth() retrieves the month, but month is zero based, so using getMonth()+1 should give you the right month. Time value seems to be ok here, albeit the hour is 23 here (GMT+1). If you want universal values, add UTC to the methods (e.g. date.getUTCFullYear(), date.getUTCHours())

var timestamp = 1301090400,
date = new Date(timestamp * 1000),
datevalues = [
   date.getFullYear(),
   date.getMonth()+1,
   date.getDate(),
   date.getHours(),
   date.getMinutes(),
   date.getSeconds(),
];
alert(datevalues); //=> [2011, 3, 25, 23, 0, 0]

这篇关于人类可读格式的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆