html - Javascript, calculating distance between two dates between months -
i'm trying calculate distance between 2 dates using javascript. right i've found great code works well, has 1 problem. whenever try calculate dates across months have 30 days instead of 31, calculates day (i guess because assumes every month has 31 days). i'm wondering if there's way fix this?
var = document.getelementbyid("from").value; var fromdate = from.slice(3, 5); fromdate = parseint(fromdate); var frommonth = from.slice(0, 2); frommonth = parseint(frommonth); var fromyear = from.slice(6, 10); fromyear = parseint(fromyear); var = document.getelementbyid("to").value; var todate = to.slice(3, 5); todate = parseint(todate); var tomonth = to.slice(0, 2); tomonth = parseint(tomonth); var toyear = to.slice(6, 10); toyear = parseint(toyear); var oneday = 24*60*60*1000; var firstdate = new date(fromyear,frommonth,fromdate); var seconddate = new date(toyear,tomonth,todate); var diffdays = math.round(math.abs((firstdate.gettime() - seconddate.gettime())/(oneday))); alert(diffdays);
use var difference= math.abs(firstdate - seconddate)
Comments
Post a Comment