js时间戳转换时间、距当前时间

 1 // 1、时间戳转化成时间格式
 2 function getTime(times) {
 3     return new Date(parseInt(times) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
 4 }
 5 
 6 // 2、时间戳转换为距离现在多长时间
 7 function getTimeUntilNow(mss) {
 8     var days = parseInt(mss / (1000 * 60 * 60 * 24));
 9     if (days > 0) {
10         return days + " 天前";
11     };
12     var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
13     if (hours > 0) {
14         return hours + " 小时前 ";
15     };
16     var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
17     if (minutes > 0) {
18         return minutes + " 分钟前 ";
19     };
20     var seconds = (mss % (1000 * 60)) / 1000;
21     return "刚刚 ";
22 }

 

posted @ 2019-01-17 11:27  电子猫  阅读(2341)  评论(0编辑  收藏  举报
博客已经出生了585天12小时9分18秒