对日期,时间,年月日的处理

对工作过程中遇到的一些日期处理进行记录。

目录

一、近一周、一月、一年

二、时间戳之差

三、获取当前年月日,星期,时分


一、近一周、一月、一年

方法一: 

示例 :

 代码如下:

getDate(){
    let dateAll = new Date();
    let year = dateAll.getFullYear();
    let month = dateAll.getMonth() + 1;
    let date = dateAll.getDate();
    this.today = year + '-' + month + '-' + date; //今天
    this.lastWeek = date > 7 ? year + '-' + month + '-' + (date-7) :                 
                             this.getLastWeek(year,month,date); //近一周
    this.lastMonth = month > 1 ? year + '-' + (month -1) + '-' + date :
                              (year - 1) + "-" + 12 + "-" + date; //近一月
    this.lastYear = (year - 1) + "-" + month + "-" + date; //近一年
},
getLastWeek(year,month,date){
    let endMonthDay = new Date(year,month-1,0).getDate(); //获取上个月的天数
    if(month > 1){
        return year + '-' + (month - 1) + '-' + (date + endMonthDay - 7);
    }else{
        return (year - 1) + '-' + 12 + '-' + (date + endMonthDay - 7);
    }
}

方法二:

如果说对日期要求不是很严格,日期范围如下示例所示:

扫描二维码关注公众号,回复: 14854605 查看本文章

 

代码如下: 

let now = new Date();
switch(e){
    case 1:
        now = new Date(now - 24 * 60*60*1000*6);
        break;
    case 2:
        now.setDate(1);
        break;
    case 3:
        now.setMonth(now.getMonth - 3 );
        now.setDate(1);
        break;
    case 4:
        now.setMonth(0);
        now.setDate(1);
}
let newMonth = ((now.getMonth() + 1)>9?(now.getMonth() + 1):'0'+(now.getMonth() + 1));
let newDate = (now.getDate()>9?now.getDate():'0'+now.getDate());
this.begin = now.getFullYear() + "-" + newMonth + "-" + newDate;
let nowEnd = new Date();
this.end = nowEnd.getFullYear() + "-" + (nowEnd.getMonth()+1) + "-" + nowEnd.getDate()

方法三:

利用时间戳进行计算,然后再转换成年-月-日。代码如下:

gettime(e){
   const start = new Date();
   this.timeInfo.end = start.getFullYear() + "-" + (start.getMonth()+1) + "-" + start.getDate();
   switch(e){
      case 1:
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);//一周
        break;
      case 2:
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);//一月
        break;
      case 3:
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);//一季度
        break;
      case 4:
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);//一年
        break;
      default:
        this.timeInfo.begin = this.timeInfo.end
     }
   this.timeInfo.begin = start.getFullYear() + "-" +(start.getMonth() + 1) + "-" + start.getDate();
      }

二、时间戳之差

 根据两个时间戳的差计算出相差多少时间(x天x时x分x秒)。

// Math.floor(x)   返回小于参数x的最大整数,即对浮点数向下取整
// Math.round(x)   四舍五入
// %               求余运算
getTime(time){  //time 为两个时间戳的相减的差
    let days = Math.floor(time/ (24 * 3600 * 1000)); //天
    let residue = time % (24 * 3600 * 1000); 
    let hours = Math.floor(residue/ (3600 * 1000));  // 小时
    let residue1 = residue % (3600 * 1000); 
    let minutes = Math.floor(residue1 / (60 * 1000)); //分钟
    let residue2 = residue1 % (60 * 1000);
    let seconds = Math.round(residue2 / 1000 ); // 秒

    let val = ((days == 0) ? "" : days + '天') + 
              ((hours == 0) ? "" : hours + '时') +
              ((minutes == 0) ? "" : minutes + '分') + 
              ((seconds == 0) ? "" : seconds + '秒');
    return val;
}

三、获取当前年月日,星期,时分

getDate(){  //获取当前年月日和星期
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let date = now.getDate();
    
    this.today = year + '年' + month + '月' + date + '日';  //当前的年月日
    
    let arr_day = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
    let day = now.getDay();
    this.week = arr_day[day];  //当前星期
},

getMin(){
    let now = new Date();
    let hour = now.getHours();
    hour = hour < 10 ? '0' + hour : hour;  //时
    let minutes = now.getMinutes();
    minutes = minutes < 10 ? '0' + minutes : minutes;  //分
    this.time = hour + ':' + minutes;  //当前 时:分
}

猜你喜欢

转载自blog.csdn.net/weixin_42464106/article/details/127547663