最近需要实现题目的功能,因为我的时间戳是PHP生成的,所以转换JS时间戳需要乘1000,废话不多说,看下面的代码把!
大家可以判断一下传进来的值是否为数值型,还有判断是否比当前的时间戳大!可以根据结果提示非法参数等等!
1 function getDateDiff(dateTimeStamp){ 2 var minute = 1000 * 60; 3 var hour = minute * 60; 4 var day = hour * 24; 5 var halfamonth = day * 15; 6 var month = day * 30; 7 8 var now = new Date().getTime(); 9 var diffValue = now - dateTimeStamp; 10 11 var monthC =diffValue/month; 12 var weekC =diffValue/(7*day); 13 var dayC =diffValue/day; 14 var hourC =diffValue/hour; 15 var minC =diffValue/minute; 16 if(hourC>=12){ 17 result = "over"; 18 } 19 else if(hourC>=1){ 20 result = parseInt(hourC) +"个小时前"; 21 } 22 else if(minC>=1){ 23 result = parseInt(minC) +"分钟前"; 24 }else 25 result = "刚刚发表"; 26 return result; 27 } 28 29 var time = getDateDiff(‘1466555123000‘); 30 alert(time);
时间: 2024-10-24 03:36:06