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

js教程评论459 views阅读模式

本篇为大家带来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时间与时间戳之间的转换实例详解的详细内容,更多请关注php教程其它相关文章!

企鹅博客
  • 本文由 发表于 2019年9月9日 04:49:13
  • 转载请务必保留本文链接:https://www.qieseo.com/391185.html

发表评论