/    Sign up×
Community /Pin to ProfileBookmark

Adding months to entered date

Hi,
I’m getting a little lost here. I need to add 18 months to a date entered on a form and show the results to the user without the time. so a date like 1/1/2000 is entered and the results should be 06/30/2001. I am parsing the string and adding 18 months of milliseconds to the date. While I have gotten this to work, it is not correct because each month has a different # of days.

Can anyone show me an easier way to add 18 months to a date entered by a user?

Thanks.

[CODE]
var str = document.form1.startdate.value;
var d1 = Date.parse(document.form1.startdate.value);
var d7 = d1+86400000*30*18;

var lm = new Date(d7);
var dt = lm.getDate();
var mm = lm.getMonth()+1;
var yy = lm.getFullYear();
var lmDate = mm + “-” + dt + “-” + yy;

document.getElementById(‘results1’).innerHTML = lmDate;

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooApr 18.2011 — // I would consider 18 months added to January 1 to be July 1 of the next year, not June 30.
[CODE]
function plus18months(d){
var d2= new Date(d);
d2.setMonth(d2.getMonth()+18);
return d2;
}

var d=new Date(2000,0,1);

alert(plus18months(d).toLocaleDateString())[/CODE]


/* returned value: Sunday, July 01, 2001 */
Copy linkTweet thisAlerts:
@coldfire421Apr 18.2011 — Try this...

[code=html]
<html>
<head>
<title>JavaScript Test</title>
<script type="text/javascript">
function init(){
var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var tmpDate = new Date();
var dateAfterEighteenMonths = null;

dateAfterEighteenMonths = tmpDate.getMonth() + 18;
if(dateAfterEighteenMonths > 11)
dateAfterEighteenMonths -= 12;

alert("Today is " + monthName[tmpDate.getMonth()] + ", 18 months from now its gonna be " + monthName[dateAfterEighteenMonths]);
}
window.onload = init;
</script>
</head>
<body>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@paulyauthorApr 18.2011 — While that is a nice idea, it is missing the day and year.

My results need to have the day, month and year.
Copy linkTweet thisAlerts:
@paulyauthorApr 18.2011 — I've got it now.

var dz=new Date(document.form1.startdate.value);

Thanks mrhoo
×

Success!

Help @pauly 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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