js返回當前日期前后某天的日期

js返回距當前日期前后某天的日期

    // getDate() 返回日期月份中的天數
    // setDate() 設置日期月份中的天數。如果傳入值超過了該月中應有的天數,則增加月份。
    function getDate(index) {
        let now = new Date(); // 當前日期
        let newDate = new Date(now);
        newDate.setDate(now.getDate() + index); // 當前日期前后的天數(由index正負決定)
        return newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate();
    }

js返回距當前日期前后某天的日期的毫秒數(時間戳)

    // getDate() 返回日期月份中的天數
    // setDate() 設置日期月份中的天數。如果傳入值超過了該月中應有的天數,則增加月份。
    // getTime() 返回表示日期的毫秒數
    function getDate(index) {
        let now = new Date(); // 當前日期
        let newDate = new Date(now);
        newDate.setDate(now.getDate() + index); // 當前日期前后的天數(由index正負決定)
        return newDate.getTime();
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容