仿微信 时间转换成昨天,前天,周几,年月日等
Posted Polaris丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仿微信 时间转换成昨天,前天,周几,年月日等相关的知识,希望对你有一定的参考价值。
var The_last=""; var time = timestampToTime(n.createTime); var timeText = getTimeText(time); //判断日期 if(The_last!=""){ time = time.replace("年","-"); time = time.replace("月","-"); time = time.replace("日",""); var oDate1 = new Date(The_last);//转换成时间戳(上一个日期) var oDate2 = new Date(time);//转换成时间戳(本日期) if(new Date().getTime()+86400000>=oDate2.getTime()&&new Date().getTime()-86400000<=oDate2.getTime()){//当天 The_last = The_last.substring(0,15);//截取年月日时分 time = time.substring(0,15);//截取年月日时分 if(The_last!=time){//判断是否是当天的数据 cen+=\'<div class="times">\'+timeText+\'</div>\' } }else{ if(oDate1.getTime()+86400000 >= oDate2.getTime()){//是否是一天内的时间 // console.log(\'上一个大\'); } else { // console.log(\'本个大\'); cen+=\'<div class="times">\'+timeText+\'</div>\' } } The_last = time; }else{ cen+=\'<div class="times">\'+timeText+\'</div>\' time = time.replace("年","-"); time = time.replace("月","-"); time = time.replace("日",""); The_last = time; } //历史时间显示 //var time = \'2018年9月29日 10:43:24\'; //时间统一函数 function getTimeText(argument) { var timeS = argument; var todayT = \'\'; // var yestodayT = \'\'; var timeCha = getTimeS(timeS); timeS = timeS.slice(-8); todayT = new Date().getHours()*60*60*1000 + new Date().getMinutes()*60*1000 + new Date().getSeconds()*1000; yestodayT = todayT + 24*60*60*1000; if(timeCha > yestodayT) { return argument.slice(0,11); } if(timeCha > todayT && timeCha < yestodayT) { return timeS.slice(0,2)>12?\'昨天 下午\'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):\'昨天 上午\'+timeS.slice(0,5); } if(timeCha < todayT) { return timeS.slice(0,2)>=12?\'下午\'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):\'上午\'+timeS.slice(0,5); } } //时间戳获取 function getTimeS(argument) { var timeS = argument; timeS = timeS.replace(/[年月]/g,\'/\').replace(/[日]/,\'\'); return new Date().getTime() - new Date(timeS).getTime() - 1000; //有一秒的误差 } //10位时间戳转换成日期 function timpToTime(timestamp) { var date = new Date(timestamp*1000);//13位就不*1000 var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? (\'\' + m) : m; var d = date.getDate(); d = d < 10 ? (\'\' + d) : d; var h = date.getHours(); h = h < 10 ? (\'0\' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? (\'0\' + minute) : minute; second = second < 10 ? (\'0\' + second) : second; return y + \'-\' + m + \'-\' + d+\' \'+h+\':\'+minute+\':\'+second; }
呈现效果:
以上是关于仿微信 时间转换成昨天,前天,周几,年月日等的主要内容,如果未能解决你的问题,请参考以下文章