获取特定年月最后一天的日期对象,datejs

Get date object of last day for specific year and month, datejs

我是新手 DateJs, and the http://www.datejs.com/ 是 530,所以我找不到每种方法的文档。

我需要获取特定年月的最后一天。例如:

我需要 2015 年 7 月的最后一天,应该是 2015 年 7 月 31 日。我尝试了以下脚本:

<script src="<%=application.getContextPath()%>/js/lib/datejs/date-zh-CN.js"></script>


 to=Date.today().set({year:2015,month:6}).final();//which returns Fri Jul 03 2015 00:00:00 GMT+0800 (中国标准时间) 

to=Date.today().set({year:2015,month:6}).final().day();//which returns Sat Jul 04 2015 00:00:00 GMT+0800 (中国标准时间)        

哪里错了(我毁剧本的那天是2015年8月3日)。我想我错误地使用了 final() 方法,但我无法从 https://github.com/datejs/Datejs 中获得直接示例。

我从未使用过 DateJS,但通过快速学习,我想这就是您想要的:

Date.today().set({year:2015,month:7,day:1}).add(-1).days()

HTH

DateJS 中有一个 moveToLastDayOfMonth 函数。

例子

Date.july().moveToLastDayOfMonth()

or,

Date.today().set({year:2015,month:6}).moveToLastDayOfMonth()

希望对您有所帮助。

有一种特定的方法可以做到这一点。显然,首先您必须设置日期,这可以在使用 now()today() 初始化的 Date 对象上完成。我建议今天将时间值归零:

Date.today().set({year:2015,month:6}).moveToLastDayOfMonth()

这将使您获得所需的确切数据。

其次,如果你使用的是 DateJS 请上帝 使用最新更新的 fork(免责声明,我维护它,我需要给它一点爱但是它 修复了原始库中的许多问题

您可以在这里找到:https://github.com/abritinthebay/datejs

它也可以作为 npm datejs 包和 bower 获得。如果你使用 NodeJS,一个简单的 npm install datejs 就可以了。