我一直在寻找如何使用 javascript 将 UNIXTIME 转换为 YY-MM-DD 格式,但到目前为止我只找到了相反的方法(YY-MM-DD 到 UNIXTIME)。
jquery 中是否已经包含任何方法或其他方法来完成这项工作?
谢谢!!
最佳答案
您可以使用像 moments.js 这样的库或者在 POJS 中
Moments.js
A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.
Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. Due to its handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line.
Summary
Creates JavaScript Date instances which let you work with dates and times.
Javascript
function padZero(number) {
if (number < 10) {
number = "0" + number;
}
return number;
}
function unixtime2YYMMDD(unixtime) {
var milliseconds = unixtime * 1000,
dateObject = new Date(milliseconds),
temp = [];
temp.push(dateObject.getUTCFullYear().toString().slice(2));
temp.push(padZero(dateObject.getUTCMonth() + 1));
temp.push(padZero(dateObject.getUTCDate()));
return temp.join("-");
}
console.log(unixtime2YYMMDD(1372069271));
输出
13-06-24
关于jsfiddle
关于javascript - 如何将unix时间转换为yy-mm-dd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354574/