欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
};
相关标签: JS javascript