化拓教育网
您的当前位置:首页js时间与时间戳之间的转换实例详解

js时间与时间戳之间的转换实例详解

来源:化拓教育网


本篇为大家带来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;
}
显示全文