简体   繁体   中英

DIfference between two dates stored in variables using javascript

I am storing the dates in these two variables. How to subtract these two dates to get number of days.Here in one variable the date has been got from the database and another variable holds the today date. Now I want to find the difference in days. some thing like var numberofdays= dt_ret-dt_due;

var dt_due= document.getElementById("duedate").value=a.four;
var dt_ret=today;

Try this:

var numberofmilliseconds = new Date( dt_due ).getTime( ) - new Date( dt_ret ).getTime( ),
    numberofdays = numberofmilliseconds / 1000 / 60 / 60 / 24;

Did that do the trick?

The date comming from database is string i consider, all you have to do is to convert your string date to js Date object and take a diffrence :

Date date1 = new Date('2015-10-28'); // DB date
Date date2 = new Date(); // current date

var diff = new Date(date2.getTime() - date1.getTime());
console.log(diff.getUTCDate() - 1); // Gives day count of difference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM