作者:乡水情缘,来自原文地址// 获取当前时间戳(以s为单位) var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
console.log("当前时间戳为:" + timestamp);
// 获取某个时间格式的时间戳 var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
console.log(stringTime + "的时间戳为:" + timestamp2);
// 将当前时间换成时间格式字符串 var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
console.log(newDate.toDateString());
console.log(newDate.toGMTString());
console.log(newDate.toISOString());
console.log(newDate.toJSON());
console.log(newDate.toLocaleDateString());
console.log(newDate.toLocaleString());
console.log(newDate.toLocaleTimeString());
console.log(newDate.toString());
console.log(newDate.toTimeString());
console.log(newDate.toUTCString());
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
//最后调用 console.log(newDate.format('yyyy-MM-dd h:m:s'));
|