我正试图形成一个日期,这是3个月前的当前日期。我通过下面的代码得到当前月份
var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;
你能给我提供一个逻辑来计算和形成一个日期(date数据类型的对象),考虑到当月份是一月(1)时,日期前3个月将是十月(10)吗?
我正试图形成一个日期,这是3个月前的当前日期。我通过下面的代码得到当前月份
var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;
你能给我提供一个逻辑来计算和形成一个日期(date数据类型的对象),考虑到当月份是一月(1)时,日期前3个月将是十月(10)吗?
当前回答
对于今天之前三个月的获取日期:
let d = new Date(new Date().setMonth(new Date().getMonth() - 3))
console.log(d.toISOString().slice(0, 10))
// 2022-05-24 (today is 2022-08-24)
其他回答
这是最小和最简单的代码。
var minDate = new Date();
minDate.setMonth(minDate.getMonth() - 3);
声明具有当前日期的变量。 然后通过使用setMonth内置函数,我们可以得到3个月后的日期。
我喜欢gilly3的简单回答,但用户可能会惊讶于3月31日之前的一个月是3月3日。我选择实现一个坚持月底的版本,所以3月28日、29日、30日和31日之前的一个月都是2月28日,因为不是闰年。
function addMonths(date, months) { var result = new Date(date), expectedMonth = ((date.getMonth() + months) % 12 + 12) % 12; result.setMonth(result.getMonth() + months); if (result.getMonth() !== expectedMonth) { result.setDate(0); } return result; } var dt2004_05_31 = new Date("2004-05-31 0:00"), dt2001_05_31 = new Date("2001-05-31 0:00"), dt2001_03_31 = new Date("2001-03-31 0:00"), dt2001_02_28 = new Date("2001-02-28 0:00"), result = addMonths(dt2001_05_31, -2); console.assert(dt2001_03_31.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2001_05_31, -3); console.assert(dt2001_02_28.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2001_05_31, 36); console.assert(dt2004_05_31.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2004_05_31, -38); console.assert(dt2001_03_31.getTime() == result.getTime(), result.toDateString()); console.log('Done.');
如果gilly3提供的setMonth方法不是你想要的,可以考虑:
var someDate = new Date(); // add arguments as needed
someDate.setTime(someDate.getTime() - 3*28*24*60*60);
// assumes the definition of "one month" to be "four weeks".
可以使用任何数量的时间,只要设置正确的倍数。
var d = new日期("2013/01/01"); console.log (d.toLocaleDateString ()); d.setMonth(d.getMonth() + 18); console.log (d.toLocaleDateString ());
传递一个JS Date对象和一个你想加/减多少个月的整数。monthsToAdd可以是正的也可以是负的。返回一个JS日期对象。
如果您的originalDateObject是3月31日,并且您传递-1作为monthsToAdd,那么您的输出日期将是2月28日。
如果您经过了大量的月份,比如36个月,它也会正确地处理年份调整。
const addMonthsToDate = (originalDateObject, monthsToAdd) => {
const originalDay = originalDateObject.getUTCDate();
const originalMonth = originalDateObject.getUTCMonth();
const originalYear = originalDateObject.getUTCFullYear();
const monthDayCountMap = {
"0": 31,
"1": 28,
"2": 31,
"3": 30,
"4": 31,
"5": 30,
"6": 31,
"7": 31,
"8": 30,
"9": 31,
"10": 30,
"11": 31
};
let newMonth;
if (newMonth > -1) {
newMonth = (((originalMonth + monthsToAdd) % 12)).toString();
} else {
const delta = (monthsToAdd * -1) % 12;
newMonth = originalMonth - delta < 0 ? (12+originalMonth) - delta : originalMonth - delta;
}
let newDay;
if (originalDay > monthDayCountMap[newMonth]) {
newDay = monthDayCountMap[newMonth].toString();
} else {
newDay = originalDay.toString();
}
newMonth = (+newMonth + 1).toString();
if (newMonth.length === 1) {
newMonth = '0' + newMonth;
}
if (newDay.length === 1) {
newDay = '0' + newDay;
}
if (monthsToAdd <= 0) {
monthsToAdd -= 11;
}
let newYear = (~~((originalMonth + monthsToAdd) / 12)) + originalYear;
let newTime = originalDateObject.toISOString().slice(10, 24);
const newDateISOString = `${newYear}-${newMonth}-${newDay}${newTime}`;
return new Date(newDateISOString);
};