1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function timeTransForm(obj){ var time = new Date(obj).getTime(); var nowTime = new Date().getTime(); var timeDiff = Math.floor((nowTime - time)/1000); var str = '' if( timeDiff < 60){ str = '刚刚' }else if(timeDiff < 600){ str = Math.floor(timeDiff/60)+'分钟前' }else if(timeDiff < 3600){ str = Math.floor(timeDiff/600)+'0分钟前' }else if(timeDiff < 3600*24){ str = Math.floor(timeDiff/3600)+'小时前' }else if(timeDiff < 3600*24*15){ str = Math.floor(timeDiff/3600/24)+'天前' }else{ str = obj } return str; } console.log(timeTransForm('2020-11-18 00:44:12'))
|