简体   繁体   English

使用javascript存储在变量中的两个日期之间的差异

[英]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 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 :来自数据库的日期是我认为的字符串,您所要做的就是将字符串日期转换为 js 日期对象并取差异:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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