Unstoppable Domains

Difference between two dates

Spaceship Spaceship
Watch

asgsoft

VIP Member
Impact
9
hey everyone!

I am trying to calculate the difference between two dates which are inputted into textboxes.

HTML:
Start Date: <input name="start_date" type="text" id="start_date" onClick="GetDate(start_date);" onChange="duration();">

End Date: <input name="end_date" type="text" id="end_date" onClick="GetDate(end_date);" onChange="duration();">

GetDate(); loads an in-line calendar while duration() is supposed to work out the difference and write it to a third textbox.

This is the Javascript I am using to try and work it out:

Code:
function days_between(date1, date2) {
    var ONE_DAY = 1000 * 60 * 60 * 24
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
    var difference_ms = Math.abs(date1_ms - date2_ms)
    return Math.round(difference_ms/ONE_DAY)
}

function duration() {
var from_date = document.getElementById('start_date').value
var to_date = document.getElementById('end_date').value
var duration = days_between(from_date, to_date)
document.getElementById('duration').value = duration
}

but the duration isn't updating at all!

Here is another function I tried which doesnt seem to work:

Code:
var day1, day2;
var month1, month2;
var year1, year2;

value1 = form1.start_date.value;
value2 = form1.end_date.value;

day1 = value1.substring (0, value1.indexOf ("/"));
month1 = value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/"));
year1 = value1.substring (value1.lastIndexOf ("/")+1, value1.length);

day2 = value2.substring (0, value2.indexOf ("/"));
month2 = value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/"));
year2 = value2.substring (value2.lastIndexOf ("/")+1, value2.length);

date1 = year1+"/"+month1+"/"+day1;
date2 = year2+"/"+month2+"/"+day2;

firstDate = Date.parse(date1)
secondDate= Date.parse(date2)

msPerDay = 24 * 60 * 60 * 1000
dbd = Math.round((secondDate.valueOf()-firstDate.valueOf())/ msPerDay) + 1;
document.getElementById('duration').value = dbd
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Just convert to timestamp and then compare and then convert back, if needed.
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back