JS 获取当前日期的前几天或者后几天
程序员文章站
2022-05-15 10:58:07
...
/**
* 计算时间
* @param {number} day 距当前天数,可传负数
* @param {boolean} isTime 是否显示具体时间
*/
export const getDay = (day, isTime) => {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds);
var tYear = today.getFullYear();
var tMonth = today.getMonth() + 1;
var tDate = today.getDate();
var tHours, tMinutes, tSeconds;
if (isTime) {
tHours = add0(today.getHours());
tMinutes = add0(today.getMinutes());
tSeconds = add0(today.getSeconds());
} else {
tHours = '00';
tMinutes = '00';
tSeconds = '00';
}
return tYear + '-' + tMonth + '-' + tDate + ' ' + tHours + ':' + tMinutes + ':' + tSeconds;
};
上一篇: linux取上月末日期