JavaScript get last month, get next month date

Source: Internet
Author: User
Tags getdate


There's a problem with writing the calendar control, using JavaScript to get the last month, there's no way to bring it back, it's too backward to write, it's tiring.


Date.getdayofmonth = function (y, Mm) {///<summary>///calculates the total number of days of the current month///create by the Yangtze River 20121225///
    t;/summary>///<returns type= "number"/>///<param name= "y" type= "number" >///year, if NULL is the current time  </param>///<param name= "Mm" type= "number" >///month, starting at 0, if NULL is the current time///</param> if (typeof y = = ' undefined ')
    {y = (new Date ()). getFullYear ();
    if (typeof mm = n ' undefined ') {mm = (new Date ()). GetMonth (); var Feb = (y% 4 = 0)?
    29:28;
    var AM = new Array (Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return AM[MM];
}; Date.getdateofpremonth = function (dt) {///<summary>///returns the date of January///create by Yangtze River 20121225///
    ;/summary>///<returns type= "date"/>///<param name= "DT" type= "date" >///reference date, if NULL is current time
    </param> if (typeof dt = = ' undefined ') {dt = (new Date ());} var y = (dt.getmonth () = = 0)? (Dt.getfullYear ()-1): Dt.getfullyear (); var m = (dt.getmonth () = = 0)?
    11:dt.getmonth ()-1; var PreM = Date.getdayofmonth<span style= "font-family:arial, Helvetica, Sans-serif;" > (y, m);</span> var d = (PreM < Dt.getdate ())?
    PreM:dt.getDate ();
return new Date (Y, M, d); };
Date.getdateofnextmonth = function (dt) {   ///<span style= "White-space:pre" > </span>< summary>    ///<span style= "White-space:pre" > </span> back to January date    ///create by Yangtze River 201 21225    ///<span style= "White-space:pre" > </span></summary>    ///<span style= "White-space:pre" > </span><returns type= "Date"/>    ///<span style= "White-space: Pre "> </span><param name=" dt "type=" Date ">    ///<span style=" White-space:pre "> </
Span> reference date, if NULL is current time    ///<span style= "White-space:pre" > </span></param>      if (typeof dt = = ' undefined ') {dt = (new Date ());}     var y = (dt.getmonth () = 11)?
(Dt.getfullyear () + 1): Dt.getfullyear ();     var m = (dt.getmonth () = = 11)?
0:dt.getmonth () + 1;
    var PreM = Date.getdayofmonth (y, M);     var d = (PreM &Lt Dt.getdate ())?
PreM:dt.getDate ();
    return new Date (Y, M, d);
 };



Suppose the current time is July 24, 2014 morning 12:00:00
//Output Result:
d.tolocalestring ()
"June 24, 2014 morning 12:00:00"
D2.tolocalestring ()
"December 28, 2013 morning 12:00:00"
DD
31


A few of the bugs have been measured, just changed

Guys, help with the test, thanks.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.