js时间与时间戳之间的转换实例详解
来源:动视网
责编:小OO
时间:2020-11-27 20:17:04
js时间与时间戳之间的转换实例详解
1.将时间戳转换成时间。var formatDate = function(d) { var now = new Date(d);var year = now.getFullYear();var month = now.getMonth() + 1;var date = now.getDate();var hour = now.getHours();var minute = now.getMinutes();var second = now.getSeconds();return year + ";-";+ month + ";-";+ date + ";";+ hour + ";:";:"。
导读1.将时间戳转换成时间。var formatDate = function(d) { var now = new Date(d);var year = now.getFullYear();var month = now.getMonth() + 1;var date = now.getDate();var hour = now.getHours();var minute = now.getMinutes();var second = now.getSeconds();return year + ";-";+ month + ";-";+ date + ";";+ hour + ";:";:"。

本篇为大家带来js时间与时间戳之间的转换实例详解,如下
1.将时间戳转换成时间
var formatDate = function(d) {
var now = new Date(d);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}2.将一个时间戳减去当前时间戳来得到秒数
var dateDifference=function(expire_time){//返回秒数
var timestamp = Date.parse(new Date());//当前时间
timestamp=timestamp/1000;
expire_time=parseInt(expire_time/1000);//过期时间
return expire_time-timestamp;
}
js时间与时间戳之间的转换实例详解
1.将时间戳转换成时间。var formatDate = function(d) { var now = new Date(d);var year = now.getFullYear();var month = now.getMonth() + 1;var date = now.getDate();var hour = now.getHours();var minute = now.getMinutes();var second = now.getSeconds();return year + ";-";+ month + ";-";+ date + ";";+ hour + ";:";:"。