原創(chuàng)聲明:本文為作者原創(chuàng),未經(jīng)允許不得轉(zhuǎn)載,經(jīng)授權(quán)轉(zhuǎn)載需注明作者和出處
轉(zhuǎn)載請(qǐng)注明出處
作者:劉冰華
2016-12-8 13:00
在公用模塊方法中添加如下轉(zhuǎn)換類:
//轉(zhuǎn)化時(shí)間成文字
function ChangeTimeToText (timeStamp){
this.now = new Date(),this.nowTime = this.now.getTime();
this.nowDay = this.now.getDate(); //當(dāng)前日
this.nowMonth = this.now.getMonth(); //當(dāng)前月
this.nowYear = this.now.getFullYear(); //當(dāng)前年
this.nowDayOfWeek = this.now.getDay(); //今天本周的第幾天
this.arr = ['日','一','二','三','四','五','六'];
this.getWeekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek).getTime();//本周開始日期
this.getCurrentDate = new Date(this.nowYear, this.nowMonth, this.nowDay).getTime();//今天開始日期
this.getTomorowDate = new Date(this.nowYear, this.nowMonth, this.nowDay+1).getTime()-1;//今天結(jié)束日期
this.nowAm = this.getCurrentDate + 12*3600*1000;//今天上午結(jié)束
this.nowPm = this.getCurrentDate + 18*3600*1000;//今天下午結(jié)束
this.nowEvening = this.getCurrentDate + 24*3600*1000;//今天晚上結(jié)束
this.yesAm = this.getCurrentDate - 24*3600*1000;//昨天上午開始
}
ChangeTimeToText.prototype.changeTimeToText = function(timeStamp){
var t = parseInt(timeStamp);
var timeStamp = new Date(timeStamp);
var nowText = 0,resu='';
var resHour = timeStamp.getHours();
var resMinutes = timeStamp.getMinutes();
var restime = (resHour < 12 ? '上午' + resHour : (resHour == 12 ? '下午' + resHour : resHour < 18 ? '下午' + (resHour-12) : '晚上' + (resHour-12))) + ':' + (resMinutes < 10 ? '0' + resMinutes : resMinutes);
if(t >= this.getTomorowDate || t < this.getWeekStartDate){
var mon = timeStamp.getMonth() + 1;
mon = mon < 10 ? '0'+mon : mon;
resu = timeStamp.getFullYear() + '年' + mon + '月' + timeStamp.getDate() + '日 ' + restime;
}else if( t >= this.getCurrentDate){
//如果是今天
resu = restime;
}else if( t >= this.yesAm){
resu = '昨天 ' + restime;
}else if( t >= this.getWeekStartDate){
resu = '星期' + this.arr[timeStamp.getDay()] +' ' + restime;
}else{
var mon = timeStamp.getMonth() + 1;
mon = mon < 10 ? '0'+mon : mon;
resu = timeStamp.getFullYear() + '年' + mon + '月' + timeStamp.getDate() + '日 ' + restime;
}
return resu;
}
//對(duì)比時(shí)間3分鐘內(nèi)創(chuàng)建新時(shí)間
ChangeTimeToText.prototype.campareTime = function(s,e){
return (e - s - 180000) > 0 ? true : false;
};
/*
* 暴露接口給外部
*/
module.exports = {ChangeTimeToText};
在用到的地方,諣用此方法
const ChangeTimeToText = new (require("../../pages/index/common")).ChangeTimeToText();
console.log(ChangeTimeToText.changeTimeToText(1480442400000),'上午2:00');
console.log(ChangeTimeToText.changeTimeToText(1480471200000),'上午10點(diǎn)');
console.log(ChangeTimeToText.changeTimeToText(1480478460000),'下午12:01');
console.log(ChangeTimeToText.changeTimeToText(1480489260000),'下午15:01');
console.log(ChangeTimeToText.changeTimeToText(1480402860000),'昨天下午15:01');
console.log(ChangeTimeToText.changeTimeToText(1480316460000),'星期一下午15:01');
console.log(ChangeTimeToText.changeTimeToText(1480230060000),'27號(hào)15:01');
console.log(ChangeTimeToText.changeTimeToText(1482832860000),'12月27下午18:01');