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();
}