在javascript中计算最后一个月的日期

编程入门 行业动态 更新时间:2024-10-25 15:30:34
本文介绍了在javascript中计算最后一个月的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果您在Date.setFullYear中提供0作为dayValue,您将获得上个月的最后一天:

if you provide 0 as the dayValue in Date.setFullYear you get the last day of the previous month:

d = new Date(); d.setFullYear(2008, 11, 0); // Sun Nov 30 2008

在 mozilla 。这是一个可靠的跨浏览器功能,还是应该看看替代方法?

There is reference to this behaviour at mozilla. Is this a reliable cross-browser feature or should I look at alternative methods?

推荐答案

var month = 0; // January var d = new Date(2008, month + 1, 0); alert(d); // last day in January IE 6: Thu Jan 31 00:00:00 CST 2008 IE 7: Thu Jan 31 00:00:00 CST 2008 IE 8: Beta 2: Thu Jan 31 00:00:00 CST 2008 Opera 8.54: Thu, 31 Jan 2008 00:00:00 GMT-0600 Opera 9.27: Thu, 31 Jan 2008 00:00:00 GMT-0600 Opera 9.60: Thu Jan 31 2008 00:00:00 GMT-0600 Firefox 2.0.0.17: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time) Firefox 3.0.3: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time) Google Chrome 0.2.149.30: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time) Safari for Windows 3.1.2: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)

输出差异是由于 toString()实现中的差异,而不是因为日期不同。

Output differences are due to differences in the toString() implementation, not because the dates are different.

当然,只是因为上面确定的浏览器使用0作为上个月的最后一天并不意味着他们会继续这样做,或者t未列出的帽子浏览器将会这样做,但它有可信度,认为它应该在每个浏览器中都是一样的。

Of course, just because the browsers identified above use 0 as the last day of the previous month does not mean they will continue to do so, or that browsers not listed will do so, but it lends credibility to the belief that it should work the same way in every browser.

更多推荐

在javascript中计算最后一个月的日期

本文发布于:2023-11-22 09:47:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一个月   日期   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!