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

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

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


当前回答

直接放入变量中的“一行代码”(多行代码,便于阅读):

var oneMonthAgo = new Date(
    new Date().getFullYear(),
    new Date().getMonth() - 1, 
    new Date().getDate()
);

其他回答

我建议使用一个名为Moment.js的库。

它经过了良好的测试,可以跨浏览器和服务器端工作(我在Angular和Node项目中都使用它)。它对区域日期有很好的支持。

http://momentjs.com/

var threeMonthsAgo = moment().subtract(3, 'months');

console.log(threeMonthsAgo.format()); // 2015-10-13T09:37:35+02:00

.format()返回ISO 8601格式的日期字符串表示形式。你也可以像这样使用自定义日期格式:format('dddd, MMMM Do YYYY, h:mm:ss a')

如果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".

可以使用任何数量的时间,只要设置正确的倍数。

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

在我的例子中,我需要减去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)

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>