admin管理员组文章数量:1287523
I have a below JS where I get the max date that is user allowed to enter from a backend script:
var max_date = new Date(pdate + " " + ptime);
where pdate and ptime is fetched from a script.
Now I need to set the min date as 1 month less than max_date
if (max_date.getMonth() == 0)
subtractyear = 1;
var min_date = new Date(Date.UTC(max_date.getFullYear() - subtractyear,max_date.getMonth() - 1,max_date.getDate(),max_date.getHours(),0,0));
My issue is that if max_date is March 31 - then will the min_date be rolled over to Feb 28 or Feb 29 or Feb 31 or Feb 30?
If the date formed is an incorrect date - say Feb 30 how can I rectify this?
I have a below JS where I get the max date that is user allowed to enter from a backend script:
var max_date = new Date(pdate + " " + ptime);
where pdate and ptime is fetched from a script.
Now I need to set the min date as 1 month less than max_date
if (max_date.getMonth() == 0)
subtractyear = 1;
var min_date = new Date(Date.UTC(max_date.getFullYear() - subtractyear,max_date.getMonth() - 1,max_date.getDate(),max_date.getHours(),0,0));
My issue is that if max_date is March 31 - then will the min_date be rolled over to Feb 28 or Feb 29 or Feb 31 or Feb 30?
If the date formed is an incorrect date - say Feb 30 how can I rectify this?
Share Improve this question edited Aug 14, 2012 at 9:29 Programmer asked Aug 14, 2012 at 9:13 ProgrammerProgrammer 8,72724 gold badges91 silver badges180 bronze badges1 Answer
Reset to default 7Simple subject the month by 1 would get a valid date -- never would setMonth
method return an invalid Date
object (the same as setDate
, setYear
and the Date
constructor):
var date = new Date(2012, 02, 31); // 2012-03-31
date.setMonth(date.getMonth() - 1); // This gets 2012-03-02
I don't quite clear what date you expected, the last day of the previous month or just the day one month earlier?
本文标签: JavascriptPrevious month computation from a current dateStack Overflow
版权声明:本文标题:Javascript - Previous month computation from a current date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741306832a2371416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论