1、获取当天日期,格式:yyyy-MM-dd

 
    getCurrentDate(n) {
      var dd = new Date();
      if (n) {
        dd.setDate(dd.getDate() - n);
      }
      var year = dd.getFullYear();
      var month =
        dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
      var day = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
      return year + "-" + month + "-" + day;
    };

this.getCurrentDate():不带参数就是默认为当天日期

this.getCurrentDate(1):参数大于0,代表为之前的某一天

this.getCurrentDate(-1):参数小于0,代表为未来的某一天

2.获取某一天的前几天或后几天

calculateDateTime(startDate,valueTime)=>{
    var date = new Date(startDate);
    var newDate = new Date(date.getFullYear(),date.getMonth(),date.getDate()+ +valueTime);
    var Y = newDate.getFullYear();
    var M = newDate.getMonth()+1;
    M = M<10?'0'+M:M;
    var D = newDate.getDate();
    D = D<10?'0'+D:D;
    return `${Y}-${M}-${D}`;
};

4.判断提供的日期是工作日还是双休日

const isWeekday = (date) => date.getDay() % 6 !== 0;
 
// new Date(年,月,日) 0是1月,以此类推
isWeekday(new Date(2021, 0, 1)) => true // 是工作日

5. 判断指定日期是否超出当天

let curDate = new Date(new Date().toLocaleDateString()).getTime() //当天
// date是传进来的日期yyyy-MM-dd
if ( new Date(date).getTime() < curDate ) {
    // 过期
} else {
    // 未过期
}

6. 获取指定日期的前(后)几日、几月、几年

// date: 指定日期,格式为yyyy-MM-dd
// type: 天、周、月、年
// number: 几
// separator: 分隔符
function getDate(date, type=null,number=0, separator="") {
    let nowdate = new Date(date);
    let y ='',m='',d = ''
    switch (type) {
        case "day":   //取number天前、后的时间
            nowdate.setTime(nowdate.getTime() + (24 * 3600 * 1000) * number);
            y = nowdate.getFullYear();
            m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
            d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
            break;
        case "week":  //取number周前、后的时间
            weekdate = new Date(nowdate + (7 * 24 * 3600 * 1000) * number);
            y = weekdate.getFullYear();
            m = weekdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
            d = weekdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
            break;
        case "month":  //取number月前、后的时间
            nowdate.setMonth(nowdate.getMonth() + number);
            y = nowdate.getFullYear();
            m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
            d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
            break;
        case "year":  //取number年前、后的时间
            nowdate.setFullYear(nowdate.getFullYear() + number);
            y = nowdate.getFullYear();
            m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
            d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
            break;
        default:     //取当前时间
            y = nowdate.getFullYear();
            m = nowdate.getMonth() + 1 < 10 ? '0' + (nowdate.getMonth() + 1) : nowdate.getMonth() + 1;
            d = nowdate.getDate() < 10 ? '0' + nowdate.getDate() : nowdate.getDate();
    }
    return y + separator + m + separator + d;
}

7. 获取当天为星期几

fmtWeekDay(date){
    if(date === "--"){
        return "--";
    }
    let dt = new Date(date.split("-")[0], date.split("-")[1] - 1,date = date.split("-")[2]);
    let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    return weekDay[dt.getDay()];
},

 

posted on 2024-04-15 16:59  鄢宁  阅读(29)  评论(0编辑  收藏  举报