/    Sign up×
Community /Pin to ProfileBookmark

javascript date substraction

hello i have this code:

[CODE]function buildMonthlyEntries() {
var startDate = new Date(document.getElementById(‘startDate’).value);
var endDate = new Date(document.getElementById(‘endDate’).value);
if(startDate == “Invalid Date” || endDate == “Invalid Date”) { return null; }
var entryCount = endDate.getMonth() – startDate.getMonth();
var monthlyEntries = document.getElementById(‘monthlyEntries’);
monthlyEntries.innerHTML = “”;
for(var i = 0; i < entryCount; i++) {
var textElement = document.createElement(‘input’);
textElement.setAttribute(‘type’, ‘text’);
textElement.setAttribute(‘id’, ‘entry’ + i);
monthlyEntries.appendChild(textElement);
}
return null;
}

<div id=”dateRange”>
<input type=”text” id=”startDate”>
<input type=”text” id=”endDate”>
</div>
<div id=”monthlyEntries”></div>
<INPUT TYPE=”button” NAME=”button” Value=”Click” onClick=”buildMonthlyEntries()”>

[/CODE]

It calculates the number of months between the 2 dates and generates the number of input filds equal to the number of months. The only problem is when i change the year. For example if i have start date 2012-03-22 and enddate 2013-04-22, instead of generating 13 input fields it only generates 1 field, because it only substracts months. How can i make this script also calculate corectly months even if year is changed? Thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsMar 22.2012 — [CODE]function buildMonthlyEntries() {
var startDate = new Date(document.getElementById('startDate').value);
var endDate = new Date(document.getElementById('endDate').value);
if(startDate == "Invalid Date" || endDate == "Invalid Date") { return null; }
var entryCount = (endDate.getMonth() - startDate.getMonth())+(endDate.getFullYear() - startDate.getFullYear())*12;
var monthlyEntries = document.getElementById('monthlyEntries');
monthlyEntries.innerHTML = "";
for(var i = 0; i < entryCount; i++) {
var textElement = document.createElement('input');
textElement.setAttribute('type', 'text');
textElement.setAttribute('id', 'entry' + i);
monthlyEntries.appendChild(textElement);
}
return null;
}
[/CODE]
Copy linkTweet thisAlerts:
@bizkit1authorMar 22.2012 — thank you!
Copy linkTweet thisAlerts:
@bizkit1authorMar 22.2012 — one more question i have for you my friend. How can i put in front the generated inputs the month name or the month number, so that i will know for which month i add data? Thanks in advance!
×

Success!

Help @bizkit1 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,
)...