首页 > 代码库 > 时间转换

时间转换

  最近遇见一个功能,需要把后台的提供的具体时间格式转换为以下格式:

  • < 1分钟 X 秒钟前
  • 1分钟-1小时 X 分钟前
  • 1小时-1 天 X 小时前
  • 1天-2天 昨天
  • 2天-3天 前天
  • 3天-7天 X 天前
  • 7天-14天 上周
  • 2周-4周 X 周前
  • 超过1个月,显示具体时间

第一次做的时候使用用户的当前系统时间减去发帖时间,计算出时间差,根据相对时间差来判断条件,修改元素的innerHTML值;代码如下:

  1     function formatdate($dateSelector){  2         var arr=[];  3         //获取时间日期  4         for(var i=0;i<$dateSelector.length;i++){  5             arr.push($dateSelector[i].innerHTML);  6         }  7         //获取系统当前时间  8         function CurrentTime(){   9             var now = new Date(); 10             var year = now.getFullYear();       // 11             var month = now.getMonth() + 1;     // 12             var day = now.getDate();            // 13             var hh = now.getHours();            // 14             var mm = now.getMinutes();          // 15             var ss= now.getSeconds(); 16             var clock = year + "-"; 17             18             if(month < 10){ 19                  clock += "0"; 20             } 21             clock += month + "-"; 22             if(day < 10){ 23                 clock += "0"; 24             } 25             clock += day + " "; 26             if(hh < 10){ 27                 clock += "0"; 28             } 29             clock += hh + ":"; 30             if(mm < 10){ 31                  clock += ‘0‘;  32             } 33             clock += mm + ":";  34             if(ss < 10){ 35                  clock += ‘0‘; 36             } 37             clock += ss; 38             return(clock);  39         }  40         CurrentTime(); 41         //计算时间差函数 42         function GetDateDiff(startTime, endTime, diffType) { 43             //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式  44             startTime = startTime.replace(/-/g, "/"); 45             endTime = endTime.replace(/-/g, "/"); 46             //将计算间隔类性字符转换为小写  47             diffType = diffType.toLowerCase(); 48             var sTime = new Date(startTime); //开始时间  49             var eTime = new Date(endTime); //结束时间  50             //作为除数的数字  51             var divNum = 1; 52             switch (diffType) { 53                 case "second": 54                     divNum = 1000;//毫秒数 55                     break; 56                 case "minute": 57                     divNum = 1000 * 60; 58                     break; 59                 case "hour": 60                     divNum = 1000 * 3600; 61                     break; 62                 case "day": 63                     divNum = 1000 * 3600 * 24; 64                     break; 65                 default: 66                     break; 67             } 68             return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum)); 69         } 70         //填写日期 71         for(var j=0; j<arr.length; j++){ 72             //小于一分钟-----X秒钟前 73             if(GetDateDiff(arr[j],CurrentTime(),"second") > 0 && GetDateDiff(arr[j],CurrentTime(),"second") <= 59){ 74                 arr[j]=GetDateDiff(arr[j],CurrentTime(),"second") + "秒钟前";  75                 $dateSelector[j].innerHTML=arr[j]; 76             } 77             //小于一小时-----X分钟前 78             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 3599){ 79                 arr[j]=Math.floor(GetDateDiff(arr[j],CurrentTime(),"second") / 60) + "分钟前"; 80                 $dateSelector[j].innerHTML=arr[j]; 81             } 82             //一小时到一天-----X小时前 83             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 86399){ 84                 arr[j]=Math.floor(GetDateDiff(arr[j],CurrentTime(),"second") / 3600) + "小时前"; 85                 $dateSelector[j].innerHTML=arr[j]; 86             } 87             //1-2天-----昨天 88             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 172799){ 89                 $dateSelector[j].innerHTML = "昨天"; 90             } 91             //2-3天-----前天 92             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 259199){ 93                 $dateSelector[j].innerHTML = "前天"; 94             } 95             //3-7天-----X天前 96             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 604799){ 97                 arr[j]=Math.floor(GetDateDiff(arr[j],CurrentTime(),"second") / 86400)+ "天前"; 98                 $dateSelector[j].innerHTML=arr[j]; 99             }100             //7-14天-----上周101             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 1209599){102                 $dateSelector[j].innerHTML="上周";103             }104             //2-4周-----X周前105             else if(GetDateDiff(arr[j],CurrentTime(),"second") <= 2419199){106                 arr[j]=Math.floor(GetDateDiff(arr[j],CurrentTime(),"second") / 604800 )+ "周前";107                 $dateSelector[j].innerHTML=arr[j];108             }109             //超过一个月-----显示具体时间110             else if(GetDateDiff(arr[j],CurrentTime(),"second") >= 2419200){111                 //arr[j]=arr[j];112                 $dateSelector[j].innerHTML=arr[j].substring(0,16);113             }114         }115     }

但是,昨天听到一个message,有国外用户访问网站时出现了负的时间值,然后,打算用第二种方法,利用时间戳来计算时间差,代码更简单;代码如下:

 1     function formatdate($dateSelector){ 2         var arr=[]; 3         //1.获取用户时间戳 4         for(var i=0;i<$dateSelector.length;i++){ 5             arr.push($dateSelector[i].innerHTML); 6         } 7         //2.获取当前时间戳-----秒数 8         var timestamp = Math.round(new Date().getTime() / 1000); 9 10         //3.转换日期格式11         function   getLocalTime(now)   {   12             var year = now.getFullYear();       //13             var month = now.getMonth() + 1;     //14             var day = now.getDate();            //15             var hh = now.getHours();            //16             var mm = now.getMinutes();          //17             var clock = year + "-";18            19             if(month < 10){20                  clock += "0";21             }22             clock += month + "-";23             if(day < 10){24                 clock += "0";25             }26             clock += day + " ";27             if(hh < 10){28                 clock += "0";29             }30             clock += hh + ":";31             if(mm < 10){32                  clock += ‘0‘; 33             }34             clock += mm + "";35             return clock;    36         }              37 38         //4.填写日期--计算差值39         for(var j=0; j<arr.length; j++){40 41             var diff = timestamp - arr[j] , day_diff = Math.floor( diff / 86400 );42 43             //小于一分钟-----X秒钟前44             if(diff > 0 && diff < 60){45                 arr[j]= diff+‘秒钟前‘;46                 $dateSelector[j].innerHTML=arr[j];47             }48             //小于一小时-----X分钟前49             else if(diff < 3600){50                 arr[j]= Math.floor(diff/60)+‘分钟前‘;51                 $dateSelector[j].innerHTML=arr[j];52             }53             //一小时到一天-----X小时前54             else if(diff < 86400){55                 arr[j]= Math.floor(diff/3600)+‘小时前‘;56                 $dateSelector[j].innerHTML=arr[j];57             }58             //1-2天-----昨天59             else if(day_diff < 2){60                 arr[j]= ‘昨天‘;61                 $dateSelector[j].innerHTML=arr[j];62             }63             //2-3天-----前天64             else if(day_diff < 3){65                 arr[j]= ‘前天‘;66                 $dateSelector[j].innerHTML=arr[j];67             }68             //3-7天-----X天前69             else if(day_diff < 7){70                 arr[j]= day_diff+‘天前‘;71                 $dateSelector[j].innerHTML=arr[j];72             }73             //7-14天-----上周74             else if(day_diff < 14){75                 arr[j]= ‘上周‘;76                 $dateSelector[j].innerHTML=arr[j];77             }78             //2-4周-----X周前79             else if(day_diff < 30){80                 arr[j]= Math.floor(day_diff/7)+‘周前‘;81                 $dateSelector[j].innerHTML=arr[j];82             }83             //超过一个月-----显示具体时间84             if(day_diff >= 30){85                 var d=new Date(parseInt(arr[j]) * 1000);86                 $dateSelector[j].innerHTML=getLocalTime(d);87             }88         }89     }