[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!
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/ :wave:
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.
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/
:wave:
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!