将时间戮转化上午,下午,星期几等文字

  • • 发表于 8年前
  • • 作者 刘冰华
  • • 4305 人浏览
  • • 2 条评论
  • • 最后编辑时间 8年前
  • • 来自 [技 术]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

转载请注明出处

作者:刘冰华
2016-12-8 13:00

在公用模块方法中添加如下转换类:

  //转化时间成文字
function ChangeTimeToText (timeStamp){

      this.now = new Date(),this.nowTime = this.now.getTime();
      this.nowDay = this.now.getDate();              //当前日
      this.nowMonth = this.now.getMonth();           //当前月
      this.nowYear = this.now.getFullYear();             //当前年

      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;//今天结束日期
      this.nowAm = this.getCurrentDate + 12*3600*1000;//今天上午结束
      this.nowPm = this.getCurrentDate + 18*3600*1000;//今天下午结束
      this.nowEvening = this.getCurrentDate + 24*3600*1000;//今天晚上结束
      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;
}
//对比时间3分钟内创建新时间
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点');
     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号15:01');
     console.log(ChangeTimeToText.changeTimeToText(1482832860000),'12月27下午18:01');
分享到:
2条评论
Ctrl+Enter
作者

刘冰华

刘冰华

APP:0 帖子:15 回复:29 积分:953

已加入社区[2921]天

屌丝男士

作者详情》
Top