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

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

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


当前回答

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复制和粘贴!您可以自己开发代码来完成您所需要的工作!

其他回答

因为我似乎没有看到它已经建议....

const d = new Date();
const day = d.getDate();
const goBack = 3;
for (let i = 0; i < goBack; i++) d.setDate(0);
d.setDate(day);

这将为您提供3个月前的今天的日期,因为. setdate(0)将日期设置为上个月的最后一天,而不管一个月包含多少天。Day用于恢复今天的日期值。

传递一个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);
};
Following code give me Just Previous Month From Current Month even the date is 31/30 of current date and last month is 30/29/28 days:

   <!DOCTYPE html>
<html>
<body>

<p>Click the button to display the date after changing the month.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {

var d = new Date("March 29, 2017"); // Please Try the result also for "March 31, 2017" Or "March 30, 2017"
var OneMonthBefore =new Date(d);

OneMonthBefore.setMonth(d.getMonth(),0);
if(OneMonthBefore.getDate() < d.getDate()  )
{
d.setMonth(d.getMonth(),0);
}else
{
d.setMonth(d.getMonth()-1);

}

    document.getElementById("demo").innerHTML = d;
}
</script>

</body>
</html>

如果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 minDate = new Date(); 
   minDate.setMonth(minDate.getMonth() - 3);

声明具有当前日期的变量。 然后通过使用setMonth内置函数,我们可以得到3个月后的日期。