...

View Full Version : javascript date substraction



bizkit1
03-22-2012, 08:48 AM
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()">

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

Philip M
03-22-2012, 09:04 AM
Evaluate endYear minus StartYear, then increment monthlyEntries by 12 x that number. 2013-2012 = 1 so increment by 1 x 12 (months).

If the number of months is inclusive:-
Start 3/2012 End 5/2012: endMonth+1 - startMonth = 3 fields, March April May
Start 11/2012 End 3/2013: endMonth+1 - StartMonth = -7, plus 12 (one year) = 5 fields

var endDate = new Date(document.getElementById('endDate').value);
Prefer not to use the same name for a Javascript variable and an HTML element.

You need to check that the entered dates are valid (not 56th February).

Quizmaster: A peak in the Andes was confirmed by an expedition in 2000 to be the source of which river?
Contestant: The Nile.

felgall
03-22-2012, 09:33 AM
Subtract one date from the other and save the result as a "date". What you will actually get if you (getFullYear() * 12) + getMonth() +1 from that result will be the number of months difference between the two dates (rounded down).


start_date = new Date(document.getElementById('startDate').value);
end_date = new Date(document.getElementById('endDate').value);
diff = new Date();
diff = end_date - start_date;
entryCount = (diff.getFullYear() * 12) + diff.getMonth() + 1;

bizkit1
03-22-2012, 11:35 AM
thank you, but i have a problem putting it all together:


function buildMonthlyEntries() {
var startDate = new Date(document.getElementById('datastart').value);
var endDate = new Date(document.getElementById('dataend').value);
diff = new Date();
diff = endDate - startDate;
if(startDate == "Invalid Date" || endDate == "Invalid Date") { return null; }
var entryCount = (diff.getFullYear() * 12) + diff.getMonth() + 1;
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;
}

can you please help me with this one? because i am new to javascript

bizkit1
03-23-2012, 09:54 AM
ok i managed to make it work:



function buildMonthlyEntries() {
var startDate = new Date(document.getElementById('datastart').value);
var endDate = new Date(document.getElementById('dataend').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('name', 'entry[]');
monthlyEntries.appendChild(textElement);

}
return null;
}

But would it be posible to add in front of every generated input field the month name?

Old Pedant
03-24-2012, 01:44 AM
Can I suggest an entirely different way?


// user full month names if you prefer
var monthNames = [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ];

function buildMonthlyEntries() {
var startDate = new Date(document.getElementById('datastart').value);
var endDate = new Date(document.getElementById('dataend').value);
if(startDate == "Invalid Date" || endDate == "Invalid Date") { return null; }

var monthlyEntries = document.getElementById('monthlyEntries');
monthlyEntries.innerHTML = "";

// uncomment next line if you want the loop to be INCLUSIVE of the endDate:
// endDate.setMonth( endDate.getMonth() + 1 );

// start with startDate; loop until we reach endDate
for( var dt = startDate;
! ( dt.getFullYear() == endDate.getFullYear() && dt.getMonth() == endDate.getMonth() );
dt.setMonth( dt.getMonth() + 1 )
) {
monthlyEntries.append( document.createTextNode(
monthNames(dt.getMonth()) + " " + String(dt.getFullYear()).substring(2)
);
var textElement = document.createElement('input');
textElement.setAttribute('type', 'text');
textElement.setAttribute('name', 'entry[]');
monthlyEntries.appendChild(textElement);
}
return null;
}


See? Why count the months. Just keep bumping the date until you reach the endDate.

The loop as given will be exclusive of the endDate. If you want it to be inclusive, just uncomment the indicated line.

bizkit1
03-24-2012, 06:49 AM
thank you for your reply, i copied the code but it doesn`t seem to work, it does not generate inputs anymore. Maybe i`m missing something?

Old Pedant
03-24-2012, 07:38 AM
Sorry. A couple of typos. Easy to find with any debugger.

This is tested and works, now:


// user full month names if you prefer
var monthNames = [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ];

function buildMonthlyEntries() {
var startDate = new Date(document.getElementById('datastart').value);
var endDate = new Date(document.getElementById('dataend').value);
if(startDate == "Invalid Date" || endDate == "Invalid Date") { return null; }

var monthlyEntries = document.getElementById('monthlyEntries');
monthlyEntries.innerHTML = "";

// uncomment next line if you want the loop to be INCLUSIVE of the endDate:
// endDate.setMonth( endDate.getMonth() + 1 );

// start with startDate; loop until we reach endDate
for( var dt = startDate;
! ( dt.getFullYear() == endDate.getFullYear() && dt.getMonth() == endDate.getMonth() );
dt.setMonth( dt.getMonth() + 1 )
) {
monthlyEntries.appendChild( document.createTextNode(
monthNames[dt.getMonth()] + " " + String(dt.getFullYear()).substring(2)
) );
var textElement = document.createElement('input');
textElement.setAttribute('type', 'text');
textElement.setAttribute('name', 'entry[]');
monthlyEntries.appendChild(textElement);
// next line optional...may or may not need it depending on your CSS
monthlyEntries.appendChild(document.createElement("br"));
}
return null;
}

bizkit1
03-24-2012, 09:00 AM
You're my hero :)
Thanks a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum