關(guān)于時間的方法

1.獲取前后幾天的時間(年月日)

function getNowFormatDate(num) {
    var today = new Date();
    var time=today.getTime() + 1000*60*60*24*num;
    var date=new Date(time);
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate()+num;
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

2.獲取當前時間(年月日時分秒)

function dateFormat(date){
      console.log(date)
      const daterc = date
      if(daterc!=null){
        const dateMat= new Date(daterc);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const hh = dateMat.getHours();
        const mm = dateMat.getMinutes();
        const ss = dateMat.getSeconds();
        return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day+" "+ (hh < 10 ? "0" : "") + hh + ":" + (mm < 10 ? "0" : "") + mm+ ":" + (ss < 10 ? "0" : "") + ss;
      }
    }
function getNowFormatDate() {
    var date = new Date();
    // var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var hours = date.getHours(); // 獲取當前小時數(shù)(0-23)
    var minutes = date.getMinutes(); // 獲取當前分鐘數(shù)(0-59)
    var seconds = date.getSeconds(); // 獲取當前秒數(shù)(0-59)
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    var currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + hours + ":" + minutes + ":" + seconds;
    return currentdate;
}

3.獲取今天星期幾

var str = '今天是星期' + '日一二三四五六'.charAt(new Date().getDay())
    console.log(str)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容