获取时间戳
var timestamp = Date.parse(new Date());
获取时间
1【 var myDate = new Date();
alert(myDate.toLocaleString());//获取日期和时间
】
2【
var now = new Date();
var yy = now.getFullYear(); //年
var mm = now.getMonth() + 1; //月
var dd = now.getDate(); //日
var hh = now.getHours(); //时
var ii = now.getMinutes(); //分
var ss = now.getSeconds(); //秒
var clock = yy + "-";
if(mm < 10) clock += "0";
clock += mm + "-";
if(dd < 10) clock += "0";
clock += dd + " ";
if(hh < 10) clock += "0";
clock += hh + ":";
if (ii < 10) clock +='0';
clock += ii + ":";
if (ss < 10) clock += '0';
clock += ss;
alert(clock);
】
时间戳转时间
函数:
//时间戳转换
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;
}
调用:
var nDate = new Date();
nDate.setTime(timestamp * 1000);
alert( nDate.format('yyyy-MM-dd h:m'));
时间转时间戳
var date = ‘2015-03-05 17:59:00.0‘;
date = date.substring(0,19);
date = date.replace(/-/g,‘/‘);
var timestamp = new Date(date).getTime();
document.write(timestamp);