js各种时间转化

中国标准时间转化成yyyy-MM-dd格式

例如Wed Dec 02 2020 00:00:00 GMT+0800 (中国标准时间)

//value要转化的标准时间
      const dateTme = new Date(value)
      const Y = dateTme.getFullYear()
      const M = dateTme.getMonth() + 1 < 10 ? "0" + (dateTme.getMonth() + 1) : dateTme.getMonth() + 1
      const D = dateTme.getDate() < 10 ? "0" + dateTme.getDate() : dateTme.getDate()
      const h = dateTme.getHours()< 10 ? "0" + dateTme.getHours() : dateTme.getHours()
      const m = dateTme.getMinutes() < 10 ? "0" + dateTme.getMinutes() : dateTme.getMinutes()
      const s = dateTme.getSeconds() < 10 ? "0" + dateTme.getSeconds() : dateTme.getSeconds()
      return  Y + "-" + M + "-" + D + " " + h + ":" + m  + ":" + s

获取当前天的前几天的时间

  var dd = new Date();
  dd.setDate(dd.getDate()+(-value));
   var y = dd.getFullYear(); 
   var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0
   var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0
   return y+"-"+m+"-"+d; 

当前月的前几个月的第一天

  function getMonthFristDay(value){
    
    
    value = value-1
    var date=new Date();                //当前时间
    var currentYear=date.getFullYear();    //获取完整的年份
    var currentMonth=date.getMonth();    //当前月份(0-11,0代表1月)
    var newMonth;                        //当前月的前几个月
    var newMonthFirstDay;                //当前月的前几个月的第一天
    if(currentMonth < value){
    
    
        currentYear -= 1;                    //年份减1
        newMonth = (currentMonth + 12 - value)<10 ?'0'+(currentMonth + 12 - value) :(currentMonth + 12 - value);    //当前月的前几个月
    } else {
    
    
        newMonth = (currentMonth - value) <10?'0'+(currentMonth - value) :(currentMonth - value);    //当前月的前几个月
    }
    newMonthFirstDay = currentYear + "-" +newMonth +"-" + '01'   //当前月的前几个月的第一天
    return newMonthFirstDay;
}

获取两个日期之间有多少天

function comparison(str) {
    
    
    //首先将日期分隔 ,获取到日期部分 和 时间部分
    // console.log(str)
    var arr = str.split(' ');
    //获取日期部分的年月日
    var day = arr[0].split('-');
    //获取时间部分的 时分秒
    var mi = arr[arr.length - 1].split(':');
    //获取当前date类型日期
    var date = new Date();
    //给date赋值  年月日
    date.setUTCFullYear(days[0], days[1] - 1, days[2]);
    //给date赋值 时分秒  首先转换utc时区 :+8
    date.setUTCHours(mi[0] - 8, mi[1], mi[2]);
    return date;
}
      const start1 = this.$comparison(end).getTime(); 
      const end1 = this.$comparison(start).getTime();
      const dateResult = ((end1 - start1) * 1.0) / (1000 * 60 * 60 * 24);
      if (dateResult > 15) {
    
    
        return true;
      }
      return false;

猜你喜欢

转载自blog.csdn.net/qq_32881447/article/details/110874875