几分钟前 几小时前 几天前... 7个阶段
function getDateDiff(dateTimeStamp){ var minute = 1000*60; var hour = minute*60; var day = hour*24; var halfmonth = day*15; var month = day*30; var now = new Date().getTime(); var diffValue = now - dateTimeStamp; if(diffValue < 0 ){ alert('结束日期不能小于开始日期'); } var monthC = diffValue/month; var weekC = diffValue/(7*day); var dayC= diffValue/day; var hourC = diffValue/hour; var minC = diffValue/minute; // 7个显示阶段 if(monthC > 12){// 大于12个月显示年-月-日 时:分 console.log(getLocalTime(dateTimeStamp)); console.log(dateTimeStamp); return getLocalTime(dateTimeStamp); }else if(monthC >= 1){// 一年以内显示 几个月前 console.log(parseInt(monthC) + '个月前'); return parseInt(monthC) + '个月前'; }else if(weekC >= 1){// 几周前 console.log(parseInt(weekC) + '周前'); return parseInt(weekC) + '周前'; }else if(dayC >= 1){// 几天前 console.log(parseInt(dayC) + '天前'); return parseInt(dayC) + '天前'; }else if(hourC >= 1){// 几小时前 console.log(parseInt(hourC) + '个小时前'); return parseInt(hourC) + '个小时前'; }else if(minC >= 1){// 几分钟前 console.log(parseInt(minC) + '分钟前'); return parseInt(minC) + '分钟前'; }else{// 刚刚 console.log('刚刚'); return '刚刚'; } // 时间戳转化为日期格式 function getLocalTime(time) { var gnow = new Date(time); var gyear = gnow.getFullYear(); var gmonth = gnow.getMonth()+1; var gday = gnow.getDate(); var ghour = gnow.getHours(); var gminute = gnow.getMinutes(); var gsecond = gnow.getSeconds(); function addZera(i){// 格式改为00 if(i<10 && i>=0){ i='0'+i; }else if(i<0){ i='00' } return i; } return gyear + "-" + addZera(gmonth) + "-" + addZera(gday) + " " + addZera(ghour) + ":" + addZera(gminute) + ":" + addZera(gsecond); } }
原文链接:http://www.qdfuns.com/notes/19055/431cb89442a5350fd82d6c0b05818078.html