dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: JavaScript: Add month in Date

  1. #1
    Join Date
    Mar 2007
    Posts
    77

    Question JavaScript: Add month in Date

    Hi all

    I want to add one month in current date, I tried lot but did not get it.
    for example: If today is 28 july 2008 then after adding one month it should 28 Aug 2008

    I m using c# as server side scripting which has the function for this but didnt found in javascript

    If any body knows plz help me

    Thank u

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: JavaScript: Add month in Date

    Something like this :
    HTML Code:
    <html>
    <head>
    <title>Example</title>
    
    </head>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    
      var currDate  = new Date();
      var currDay   = currDate.getDate();
      var currMonth = currDate.getMonth() + 1;
      var currYear  = currDate.getYear();
      currDateStr   = currMonth + "/" + currDay + "/" + currYear;
      document.write(currDateStr);
      document.write("<p>");
      var ModMonth = currMonth + 1;
      if (ModMonth > 12)
       { 
         ModMonth = 1;
         currYear = currYear + 1;
       }
    
      ModDateStr = ModMonth + "/" + currDay + "/" + currYear
      document.write(ModDateStr);
    </SCRIPT>
    </body>
    </html>
    May not be the prettiest & best way though...
    Last edited by HanneSThEGreaT; July 28th, 2008 at 08:31 AM.
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  3. #3
    Join Date
    Dec 2008
    Posts
    1

    Re: JavaScript: Add month in Date

    The script above will each year fail on 30 Januari if the user displays the date one month further.
    It is also not compatible with leap years.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: JavaScript: Add month in Date

    Care to explain more as to why this would be &#191;
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  5. #5
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    Re: JavaScript: Add month in Date

    Quote Originally Posted by HanneSThEGreaT View Post
    Care to explain more as to why this would be
    I think he meant to say that if today is January 30th, and you add one month, you get February 30th, which would be incorrect. Solving this will also solve the leap years problem.

    However, I am not sure if the Date object itself will take care of such issues. Most likely it does. I'll have to test this.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: JavaScript: Add month in Date

    Thanx k:arel for pointing that out, and thanx marceln for helping me understand. To be honest, when Iworked out the above sample, the issue of leap years never even crossed my mind .

    I did a quick fix. With the next code snippet, I determine whether or not we have a leap year ( inside the getNumFebDays function ), then, based on that result - either 28 or 29, I used a variable named ModDay to display the correct day inside March. It may not be a perfect solution, but it should give an idea on what to do. Here goes :
    PHP Code:
    <html>
    <
    head>
    <
    title>Example</title>

    </
    head>
    <
    body>
    <
    SCRIPT LANGUAGE="JavaScript">

      var 
    currDate  = new Date();
      var 
    currDay   currDate.getDate();
      var 
    currMonth currDate.getMonth() + 1;
      var 
    currYear  currDate.getYear();
      
    currDateStr   currMonth "/" currDay "/" currYear;
      
    document.write(currDateStr);
      
    document.write("<p>");
      var 
    ModMonth currMonth 1;
      if (
    ModMonth 12)
       { 
         
    ModMonth 1;
         
    currYear currYear 1;
       }
      
    var 
    ModDay
      
    if (getNumFebDays(currYear) == 28 && currMonth == ) {
    ModDay }
      if (
    getNumFebDays(currYear) == 29 && currMonth == 2) {
    ModDay }

      
    ModDateStr ModMonth "/" ModDay "/" currYear
      document
    .write(ModDateStr);

    function 
    getNumFebDays(theYear)      // calc num days in February
      
    {
      if ((
    theYear &#37; 4 == 0 && theYear % 100 != 0) || theYear % 400 == 0)
        
    return 29
      
    else
        return 
    28
      
    }
    </
    SCRIPT>
    </
    body>
    </
    html
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  7. #7
    Join Date
    Feb 2008
    Posts
    966

    Re: JavaScript: Add month in Date

    Why not use the built in functions for this?

    Code:
    var myDate = new Date(document.getElementById("dateField").value);
    myDate.setMonth( myDate.getMonth() + 1 );
    The Date object will wrap the year around and increment it if the current month is December. I am not sure why you need all of that extra validation unless for January 30th you want Feb 28th maybe?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)