javascript 如何使用javascript验证日期是否是本月的最后一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6355063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:23:40  来源:igfitidea点击:

How to validate date if is the last day of the month with javascript?

javascript

提问by Fire Hand

How to validate user input date is the last day of the month using javascript?

如何使用javascript验证用户输入日期是本月的最后一天?

回答by T.J. Crowder

(Update: See the final example at the bottom, but the rest is left as background.)

更新:请参阅底部的最后一个示例,但其余部分留作背景。)

You can add a day to the Dateinstance and see if the month changes (because JavaScript's Dateobject fixes up invalid day-of-month values intelligently), e.g.:

您可以在Date实例中添加一天并查看月份是否发生变化(因为 JavaScript 的Date对象智能地修复了无效的日期值),例如:

function isLastDay(dt) {
    var test = new Date(dt.getTime()),
        month = test.getMonth();

    test.setDate(test.getDate() + 1);
    return test.getMonth() !== month;
}

Gratuitous live example

无偿的活生生的例子

...or as paxdiablopointed out, you can check the resulting day-of-month, which is probably faster (one fewer function call) and is definitely a bit shorter:

...或者正如paxdiablo指出的那样,您可以检查结果的月份日期,这可能更快(少一个函数调用)并且肯定更短:

function isLastDay(dt) {
    var test = new Date(dt.getTime());
    test.setDate(test.getDate() + 1);
    return test.getDate() === 1;
}

Another gratuitous live example

另一个免费的活生生的例子

You could embed more logic in there to avoid creating the temporary date object if you liked since it's reallyonly needed in February and the rest is just a table lookup, but the advantage of both of the above is that they defer alldate math to the JavaScript engine. Creating the object is not going to be expensive enough to worry about.

如果您愿意,您可以在其中嵌入更多逻辑以避免创建临时日期对象,因为它确实只在二月份需要,其余的只是一个表查找,但上述两者的优点是它们将所有日期数学推迟到JavaScript 引擎。创建对象的成本不会高到让人担心。



...and finally: Since the JavaScript specificationrequires (Section 15.9.1.1) that a day is exactly86,400,000 milliseconds long (when in reality days vary in lengtha bit), we can make the above even shorter by adding the day as we :

...最后:由于JavaScript 规范要求(第 15.9.1.1 节)一天的长度恰好为86,400,000 毫秒(实际上天的长度略有不同),我们可以通过添加一天来使上述内容更短:

function isLastDay(dt) {
    return new Date(dt.getTime() + 86400000).getDate() === 1;
}

Final gratuitous example

最后一个免费的例子

回答by VAShhh

Browsers identify the day 0 as the last day of the previous month

浏览器将第 0 天识别为上个月的最后一天

var month = 0; // January
var d = new Date(2008, month + 1, 0);
alert(d); // last day in January

as seen here : Calculate last day of month in javascript

如下所示:在javascript中计算一个月的最后一天

you can simply use the month inserted by the user + 1with day = 0to check if he has inserted the last day of the month.

您可以简单地使用用户插入的月份 + 1天 = 0来检查他是否插入了该月的最后一天。

回答by SelimOber

Suppose that you got:

假设你有:

var month = getMonthFromUserInput();
var year = getYearFromUserInput();

This will give the last day of that month

这将给出该月的最后一天

new Date((new Date(year , month , 1)) -1 )

Remember that month 0 is Jan

记住 0 月是一月

回答by Justin

This is worked.

这是有效的。

var   lastday   =   new   Date(new   Date().getFullYear(),new   Date().getMonth()+1,0).getDate();

Hope help you.

希望能帮到你。

回答by Kanad Chourasia

function isLastDayOfMonth(date){
    return date.getDate() == new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
}