/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] months between two dates

HI

I’m trying to figure out how to use js to calculate the number of months left between two date.

On a form I will have a date in the past

var termStartDate = document.mortgagepenalty.termStartDate.value

so I want today – termStartDate = x months

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 09.2006 — The form date needs to be an "mm/dd/yyyy" format. The following presumes that the user-specified date will actually be prior to the current date:
<i>
</i>var usrDate = new Date(termStartDate);
var curDate = new Date();
var usrYear, usrMonth = usrDate.getMonth()+1;
var curYear, curMonth = curDate.getMonth()+1;
if((usrYear=usrDate.getFullYear()) &lt; (curYear=curDate.getFullYear())){
curMonth += (curYear - usrYear) * 12;
}
var diffMonths = curMonth - usrMonth;
if(usrDate.getDate() &gt; curDate.getDate()) diffMonths--;
alert("There are " + diffMonths + " months between " + usrDate + " and " + curDate);
Copy linkTweet thisAlerts:
@kprocauthorApr 10.2006 — thank you

that worked excellent
Copy linkTweet thisAlerts:
@nlm_murthyAug 24.2006 — Please help in finding out , How do you change number of months to dd/mm/yyyy format?

Example: August 24 2006 minus 0 years 10 months 0 days would be October 24 2005

sysdate-numberofmonth=dateformat
Copy linkTweet thisAlerts:
@LeeUAug 24.2006 — Please help in finding out , How do you change number of months to dd/mm/yyyy format?

Example: August 24 2006 minus 0 years 10 months 0 days would be October 24 2005 sysdate-numberofmonth=dateformat[/QUOTE]

Asked and answered [URL=http://www.webdeveloper.com/forum/showthread.php?t=118949]here[/URL]
×

Success!

Help @kproc 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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