Results 1 to 5 of 5

Thread: [RESOLVED] Javascript: Adding Month To Current Date

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] Javascript: Adding Month To Current Date

    Hello!

    Here's the code I am using to add 6 months to today's current date.

    It uses jQuery also. When the "6months" checkbox is checked, I want to have 6 months added to the current date and then insert it into the "Date" textbox.

    [CODE]
    $("#6months").click(function(){
    if ($("#6months").is(":checked"))
    {
    var d = new Date();
    d.setMonth(d.getMonth()+7);

    $("#Date").val(d.getFullYear()+"-"+(d.getMonth())+"-"+d.getDate());
    }});
    [/CODE

    Because today's date is: 2012-06-22, the date that appears in the Date textbox is this:

    2013-0-22

    Hmmm... and, of course, I want it to be:

    2012-12-22

    Any idea what the '0' is all about or how to correct it?

    Thanks!

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Javascript: Adding Month To Current Date

    Try using this Date library. It's very flexible and easy to use: http://code.google.com/p/datejs/

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Javascript: Adding Month To Current Date

    Thank you for taking the time to respond. I was messing around with that library. I like it a lot.

    It was giving me the same problem though.

    I figured out how to solve it actually with/without your date library:

    Code:
    d.setMonth(d.getMonth()+6);
       				 
    	    	$("#Date").val(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate());
           		}});
    That seems to be working just fine now.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] Javascript: Adding Month To Current Date

    The link that I have provided to has lots of samples on the usage.

    Here's the code using that library:
    Code:
    $("#6months").click(function(){ 
      if ($("#6months").is(":checked"))
      {
          var newDate = Date.today().addMonths(6).toString('yyyy-MM-dd');
          $('#Date').val(newDate);
      }
    });
    Assuming you have downloaded and included the date.js library.

    For online demo: http://jsfiddle.net/J3cPD/


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: [RESOLVED] Javascript: Adding Month To Current Date

    Thanks. I did reference the script and used it, but for some reason I got the same issue. I can also use your suggested code above - as I see that works just fine. Thanks a lot!

Posting Permissions

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



Click Here to Expand Forum to Full Width