我正试图形成一个日期,这是3个月前的当前日期。我通过下面的代码得到当前月份

var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;

你能给我提供一个逻辑来计算和形成一个日期(date数据类型的对象),考虑到当月份是一月(1)时,日期前3个月将是十月(10)吗?


当前回答

for (let monthOfYear = 0;monthOfYear < 12;monthOfYear + +) { const maxDate = new Date(); const minDate = new Date(); const max = maxDate.setMonth(maxDate.getMonth() - (monthOfYear - 1), 0); const min = maxDate.setMonth(minDate.getMonth() - (monthOfYear), 1); console.log('max: ',新的日期(max)); console.log('min: ', new Date(min)); }

其他回答

d.setMonth在浏览器try中修改了本地时间

 const calcDate = (m) => {
    let date = new Date();
    let day = date.getDate();
    let month = date.getMonth() + 1;
    let year = date.getFullYear();
    let days = 0;

    if (m > 0) {
      for (let i = 1; i < m; i++) {
        month += 1;
        if (month > 12) {
          year += 1;
          month = 1;
        }
        days += new Date(year, month, 0).getDate();
      }
    } else {
      for (let i = m; i < 0; i++) {
        month -= 1;
        if (month < 1) {
          year -= 1;
          month = 12;
        }
        days -= new Date(year, month, 0).getDate();
      }
    }

    const newTime = date.getTime() + 3600 * 24 * 1000 * days;
    return new Date(newTime);
  };

calcDate(3)//+3 month
var d = new Date();
d.setMonth(d.getMonth() - 3);

这适用于一月份。运行这段代码:

var d =新日期(2012年1月14日); 游戏机。d . toLocaleDateString log (()); d.setMonth(d.getMonth) - 3; 游戏机。d . toLocaleDateString log (());


这里有一些警告……

一个月是一个奇怪的东西。你如何定义1个月?30天吗?大多数人会说,一个月前是指需要引用的前一个月的同一天。但超过一半的时间,是31天前,而不是30天前。如果今天是这个月的31号(而不是8月或12月),那么这个月的这一天在前一个月是不存在的。

有趣的是,如果你问谷歌哪个月比哪个月早,它会同意JavaScript的说法:

它还说一个月有30.4167天:

那么,3月31日之前的一个月和3月28日之前的一个月是同一天吗?这完全取决于你对“一个月前”的定义。去和你的产品负责人谈谈。

如果你想像momentjs那样做,并通过移动到本月的最后一天来纠正这些错误,你可以这样做:

const d =新的日期(“2019年3月31日”); console.log (d.toLocaleDateString ()); const month = d.getMonth(); d.setMonth(d.getMonth() - 1); while (d.getMonth() === month) { d.setDate(d.getDate() - 1); } console.log (d.toLocaleDateString ());

如果您的需求比这更复杂,请使用一些数学并编写一些代码。你是一个开发人员!你不需要安装一个库!你不需要从stackoverflow复制和粘贴!您可以自己开发代码来完成您所需要的工作!

对于今天之前三个月的获取日期:

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)

我喜欢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.');

在我的例子中,我需要减去1个月到当前日期。重要的部分是月号,所以它不关心你在本月的哪一天,我需要上个月。这是我的代码:

var dateObj = new Date('2017-03-30 00:00:00'); //Create new date object
console.log(dateObj); // Thu Mar 30 2017 00:00:00 GMT-0300 (ART)

dateObj.setDate(1); //Set first day of the month from current date
dateObj.setDate(-1); // Substract 1 day to the first day of the month

//Now, you are in the last month
console.log(dateObj); // Mon Feb 27 2017 00:00:00 GMT-0300 (ART)

减去1个月到实际日期是不准确的,这就是为什么在第一个地方我设置了一个月的第一天(任何一个月的第一天总是第一天),在第二个地方我减去1天,这总是把你送到上个月。 希望能帮到你,伙计。

var dateObj = new Date('2017-03-30 00:00:00');//创建新的日期对象 console.log (dateObj);// 2017年3月30日星期四00:00:00 GMT-0300 (ART) dateObj.setDate (1);//从当前日期开始设置月的第一天 dateObj.setDate (1);//每月的第一天减1 //现在是最后一个月 console.log (dateObj);// 2017年2月27日星期一00:00:00 GMT-0300 (ART)