Javascript日期:下个月

时间:2009-02-01 00:03:25

标签: javascript date

我一直在为项目使用Javascript的日期,但今天注意到我以前工作的代码不再正常工作。下面的代码不是按预期产生二月,而是产生三月。

我的代码看起来像这样:

current = new Date();
current.setMonth(current.getMonth()+1); //If today is Jan, expect it to be Feb now

此代码每天都有效,直到今天。这是一个Javascript错误还是我错误的方式?

9 个答案:

答案 0 :(得分:47)

您可能会发现您将日期设置为2009年2月31日(如果今天是1月31日)并且Javascript会自动将其设置为3月初。

检查当天的日期,我预计它会为1,2或3.如果与您添加一个月之前不一样,请回滚一天,直到月份再次更改。

这样,“1月的最后一天”成为“2月的最后一天”。

编辑:

罗纳德,基于你对其他答案的评论,你可能想要避开边缘案例行为,例如“当我试图制作2月30日时会发生什么”或“当我试图制作2009/13时会发生什么” 07(yyyy / mm / dd)“(即使我的解决方案,最后一个可能仍然是一个问题,所以你应该测试它。)

相反,我会明确地编写可能性的代码。既然你不关心这个月的哪一天(你只想让下个月的年份和月份正确),这样的事情就足够了:

var now = new Date();
if (now.getMonth() == 11) {
    var current = new Date(now.getFullYear() + 1, 0, 1);
} else {
    var current = new Date(now.getFullYear(), now.getMonth() + 1, 1);
}

在12月的任何一天以及下个月的第一天,任何其他日期都会给你次年1月1日。我知道更多的代码,但是我已经厌倦了编写效率的技巧,更喜欢可读性,除非明确要求不这样做。

答案 1 :(得分:28)

相反,请尝试:

var now = new Date();
current = new Date(now.getFullYear(), now.getMonth()+1, 1);

答案 2 :(得分:15)

我一直在寻找一个简单的单行解决方案来通过数学获得下个月,所以我不必查找javascript日期函数(我的心理懒惰)。很奇怪,我在这里找不到一个。

我克服了短暂的懒惰,写了一篇,然后决定分享!

<强>解决方案:

(new Date().getMonth()+1)%12 + 1

为了清楚为什么会这样,让我打破魔法!

它获得当前月份(以0..11格式),下个月递增1,并将其包裹到12通过模数的边界(11%12 == 11; 12%12 == 0)。这将以相同的0..11格式返回下个月,因此转换为格式Date()将识别(1..12)很简单:只需再次添加1。

概念证明:

> for(var m=0;m<=11;m++) { console.info( "next month for %i: %i", m+1, (m+1)%12 + 1 ) }
next month for 1: 2
next month for 2: 3
next month for 3: 4
next month for 4: 5
next month for 5: 6
next month for 6: 7
next month for 7: 8
next month for 8: 9
next month for 9: 10
next month for 10: 11
next month for 11: 12
next month for 12: 1

所以你有它。

答案 3 :(得分:5)

您可以使用date.js库:

http://code.google.com/p/datejs/

就这样做

Date.today().next().month();

您将获得今天+ 1个月(包括天)的确切值

答案 4 :(得分:2)

如果你使用moment.js,他们有一个添加功能。 这是链接 - https://momentjs.com/docs/#/manipulating/add/

moment().add(7, 'months');

我还根据paxdiablo的答案编写了一个递归函数来添加可变数月。默认情况下,此功能会在当前日期添加一个月。

function addMonths(after = 1, now = new Date()) {
        var current;
        if (now.getMonth() == 11) {
            current = new Date(now.getFullYear() + 1, 0, 1);
        } else {
            current = new Date(now.getFullYear(), now.getMonth() + 1, 1);            
        }
        return (after == 1) ? current : addMonths(after - 1, new Date(now.getFullYear(), now.getMonth() + 1, 1))
    }

示例

console.log('Add 3 months to November', addMonths(3, new Date(2017, 10, 27)))

输出 -

Add 3 months to November Thu Feb 01 2018 00:00:00 GMT-0800 (Pacific Standard Time)

答案 5 :(得分:2)

如果您能够使用 date-fns 库 v2+,您可以导入函数 addMonths,并使用它来获取下个月

<script type="module">
  import { addMonths } from 'https://cdn.jsdelivr.net/npm/date-fns/+esm'; 
  
  const current = new Date();
  const nextMonth = addMonths(current, 1);
  console.log(`Next month is ${nextMonth}`);
</script>

答案 6 :(得分:1)

试试这个:

var a = screen.Data.getFullYear();
var m = screen.Data.getMonth();
var d = screen.Data.getDate();

m = m + 1;
screen.Data = new Date(a, m, d);

if (screen.Data.getDate() != d)
screen.Data = new Date(a, m + 1, 0);

答案 7 :(得分:1)

你可能会这样做

var currentMonth = new Date().getMonth();

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

for(var i = currentMonth-1 ; i<= 4; i++){
console.log(monthNames[i])// make it as an array of objects here 

}

答案 8 :(得分:0)

啊,三元之美:

const now = new Date();
const expiry = now.getMonth() == 11 ? new Date(now.getFullYear()+1, 0 , 1) : new Date(now.getFullYear(), now.getMonth() + 1, 1);

只是@paxdiablo和@Tom

提供的解决方案的简单版本
相关问题