JavaScript获取时间戳、日期格式化

一、 js获取时间戳:
 
第一种方法:
var timestamp1 = Date.parse(new Date());
 
第二种方法:
var timestamp2 = (new Date()).valueOf();
 
第三种方法:
var timestamp3 = new Date().getTime();
 
alert(timestamp1);//结果:1372751992000
alert(timestamp2);//结果:1372751992066
alert(timestamp3);//结果:1372751992066
 
备注:第一种获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。
 
  二、 时间戳格式化:
 
function formatDate(now) {
var year = now.getFullYear(),
month = now.getMonth() + 1,
date = now.getDate(),
hour = now.getHours(),
minute = now.getMinutes(),
second = now.getSeconds();
 
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
 
var d = new Date();
alert(formatDate(d));
posted @ 2016-12-05 17:24  独孤之路  阅读(100)  评论(0)    收藏  举报