/    Sign up×
Community /Pin to ProfileBookmark

Add 6 months to the date

How could I output 6 months out from todays date. Currently the script gives me todays date?

<form name=”dform”>
<input type=”text” name=”currentdate” size=11>
</form>

<script>

var mydate=new Date()
var theyear=mydate.getYear()
if (theyear < 1000)
theyear+=1900
var theday=mydate.getDay()
var themonth=mydate.getMonth()+1
if (themonth<10)
themonth=”0″+themonth
var theday=mydate.getDate()
if (theday<10)
theday=”0″+theday

var displayfirst=themonth
var displaysecond=theday
var displaythird=theyear

document.dform.currentdate.value=displayfirst+”/”+displaysecond+”/”+displaythird
</script>

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@yellabuffDec 05.2006 — <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<body>

<form name="dform">

Current Date: <input type="text" name="currentdate" size=11><br>

Future Date: <input type="text" name="futuredate" size=11>

</form>

<script>

var mydate=new Date();

var fdate=new Date();

var daysToAdd = 180; //6 months

var fTime = fdate.getTime()+(daysToAdd*24*60*60*1000);

fdate.setTime(fTime);

document.dform.currentdate.value = formatDate(mydate);

document.dform.futuredate.value = formatDate(fdate);

function formatDate(d) {

var theyear=d.getYear()

if (theyear < 1000) theyear+=1900

var theday=d.getDay()
var themonth=d.getMonth()+1
if (themonth<10) themonth="0"+themonth

var theday = d.getDate()
if (theday<10) theday="0"+theday

var displayfirst=themonth
var displaysecond=theday
var displaythird=theyear

return displayfirst+"/"+displaysecond+"/"+displaythird

}

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@CharlesDec 05.2006 — &lt;script type="text/javascript"&gt;

Date.prototype.addMonths = function (m) {
var d = new Date (this.getTime())
d.setMonth (d.getMonth() + m)
return d
}

alert (new Date().addMonths(6))

&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorDec 05.2006 — Thanks Guys,

is it possible to just add the 6 months onto the months and not actually calculate the days so its always is the same day number but just 6 months ahead?


Thanks!
Copy linkTweet thisAlerts:
@CharlesDec 05.2006 — Thanks Guys,

is it possible to just add the 6 months onto the months and not actually calculate the days so its always is the same day number but just 6 months ahead?


Thanks![/QUOTE]
See my example above.
Copy linkTweet thisAlerts:
@HDCDec 06.2006 — Thanks Charles your output is exactly what I want but I've been trying to insert it into a form field like the above example. Instead of an alert I need to write it into a form box.
Copy linkTweet thisAlerts:
@CharlesDec 06.2006 — Something like:&lt;script type="text/javascript"&gt;

Date.prototype.addMonths = function (m) {
var d = new Date (this.getTime())
d.setMonth (d.getMonth() + m)
return d
}

onload = function () {document.forms.elements.dateSixMonthFromNow.value = new Date().addMonths(6)}

&lt;/script&gt;
But if you're not going to be more specific than neither am I.
Copy linkTweet thisAlerts:
@yellabuffDec 06.2006 — [code=html]<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
Date.prototype.addMonths = function (m) {
var d = new Date (this.getTime())
d.setMonth (d.getMonth() + m)
return d
}

onload = function () {
document.dform.currentdate.value = formatDate(new Date());
document.dform.futuredate.value = formatDate(new Date().addMonths(6));
}

function formatDate(d) {
var theyear=d.getYear()
if (theyear < 1000) theyear+=1900

var theday=d.getDay()
var themonth=d.getMonth()+1
if (themonth<10) themonth="0"+themonth

var theday = d.getDate()
if (theday<10) theday="0"+theday

var displayfirst=themonth
var displaysecond=theday
var displaythird=theyear

return displayfirst+"/"+displaysecond+"/"+displaythird
}
</script>
</head>

<body>
<form name="dform">
Current Date: <input type="text" name="currentdate" size=11><br>
Future Date: <input type="text" name="futuredate" size=11>
</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@WattsDec 06.2006 — 6 months onto the months and not actually calculate the days[/quote]

Just make sure you don't wind up with February 31st or someother type of weirdness.
Copy linkTweet thisAlerts:
@mjdamatoDec 06.2006 — Just make sure you don't wind up with February 31st or someother type of weirdness.[/QUOTE]

Feb 31st would evaluate to March 3rd (or March 4th on a leap year).
Copy linkTweet thisAlerts:
@CharlesDec 06.2006 — Just make sure you don't wind up with February 31st or someother type of weirdness.[/QUOTE]You'll find that my example takes care of that.
Copy linkTweet thisAlerts:
@HDCDec 07.2006 — Perfect guys thank you!
Copy linkTweet thisAlerts:
@HDCApr 03.2008 — Script works great but is it possible to add the 6 months plus the ending day of the month.

Example:

todays date = 4-3-2008

6 month date would be = 10-31-2008

the 31 or end date would replace todays date(3)

Thanks for the help
×

Success!

Help @adalby spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...