码迷,mamicode.com
首页 > 其他好文 > 详细

日期格式化多少分钟之前,多少小时之前

时间:2017-07-31 17:17:54      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:logs   for   orm   var   获取   turn   eth   日期   style   

1.日期格式化

 1 function formatDate(time){ 
 2                 var pubsh = new Date(time);    
 3                 var pubshtime = parseInt(Date.parse(new Date(time))/1000);
 4                 var now = parseInt(Date.parse(new Date())/1000);
 5                 var nowtime = new Date();    
 6                 var nowY=nowtime.getFullYear();
 7                 var nowM=parseInt(nowtime.getMonth())+1;
 8                 var nowD=nowtime.getDate();
 9                 var todaytime = parseInt(Date.parse(new Date(nowY+"-"+nowM+"-"+nowD))/1000); //今天0点0分时间戳
10                 var nowh=nowtime.getHours();
11                 var nowm=nowtime.getMinutes();
12                 var yesterdaytime = todaytime-24*60*60; //昨天0点0分时间戳
13                 var idate = ‘‘;
14                 
15                 if(pubshtime<yesterdaytime){
16                     idate = time;
17                 }else if(pubshtime>yesterdaytime && pubshtime<todaytime ){
18                     idate = ‘昨天 ‘+ (‘0‘+pubsh.getHours()).slice(-2) +‘:‘+ (‘0‘+pubsh.getMinutes()).slice(-2) ;
19                 }else{
20                     var cha = now-pubshtime;
21                     if(cha<=60){
22                         idate = cha+‘秒之前‘;
23                     }else if(cha>60 && cha<=3600){
24                         idate = parseInt(cha/60)+‘分钟之前‘;
25                     }else if(cha>3600){
26                         idate = parseInt(cha/3600)+‘小时之前‘;
27                     }
28                 }                
29                 return idate;
30             }

2.时间戳格式化

 1 function TimeLine(t1) {
 2                 var Time1 = t1;
 3                 var Time2 = Date.parse(new Date());
 4                 //获取时间秒数差值
 5                 var gap = (Time2 - Time1) / 1000;
 6                 var RealTime;
 7                 if(gap < 3600) {
 8                     RealTime = Math.round(gap / 60) + ‘分钟前‘;
 9                 } else if(gap >= 3600 && gap < 3600 * 24) {
10                     RealTime = Math.round(gap / 3600) + ‘小时前‘;
11                 } else {
12                     RealTime = format(t1);
13                 }
14                 return RealTime;
15             };

 

日期格式化多少分钟之前,多少小时之前

标签:logs   for   orm   var   获取   turn   eth   日期   style   

原文地址:http://www.cnblogs.com/manyumei/p/7263966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!