Vaibhav Deshmukh
Vaibhav Deshmukh

Reputation: 4379

How to calculate number of days between two dates

I have two input dates taking from Date Picker control. I have selected start date 2/2/2012 and end date 2/7/2012. I have written following code for that.

I should get result as 6 but I am getting 5.

function SetDays(invoker) {   
    var start = $find('<%=StartWebDatePicker.ClientID%>').get_value();
    var end = $find('<%=EndWebDatePicker.ClientID%>').get_value();

    var oneDay=1000 * 60 * 60 * 24;
    var difference_ms = Math.abs(end.getTime() - start.getTime())
    var diffValue = Math.round(difference_ms / oneDay);
}

Can anyone tell me how I can get exact difference?

Upvotes: 413

Views: 492398

Answers (8)

Moctar Yonli
Moctar Yonli

Reputation: 157

// today
const date = new Date();

// tomorrow
const nextDay = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

// Difference in time
const Difference_In_Time = nextDay.getTime() - date.getTime();

// Difference in Days 
const Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

Upvotes: 0

Phil
Phil

Reputation: 713

I made a quick re-usable function in ES6 using Moment.js.

const getDaysDiff = (start_date, end_date, date_format = 'YYYY-MM-DD') => {
  const getDateAsArray = (date) => {
    return moment(date.split(/\D+/), date_format);
  }
  return getDateAsArray(end_date).diff(getDateAsArray(start_date), 'days') + 1;
}

console.log(getDaysDiff('2019-10-01', '2019-10-30'));
console.log(getDaysDiff('2019/10/01', '2019/10/30'));
console.log(getDaysDiff('2019.10-01', '2019.10 30'));
console.log(getDaysDiff('2019 10 01', '2019 10 30'));
console.log(getDaysDiff('+++++2019!!/###10/$$01', '2019-10-30'));
console.log(getDaysDiff('2019-10-01-2019', '2019-10-30'));
console.log(getDaysDiff('10-01-2019', '10-30-2019', 'MM-DD-YYYY'));

console.log(getDaysDiff('10-01-2019', '10-30-2019'));
console.log(getDaysDiff('10-01-2019', '2019-10-30', 'MM-DD-YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Upvotes: 5

Martin Kravec
Martin Kravec

Reputation: 312

This works for me:

const from = '2019-01-01';
const to   = '2019-01-08';

Math.abs(
    moment(from, 'YYYY-MM-DD')
      .startOf('day')
      .diff(moment(to, 'YYYY-MM-DD').startOf('day'), 'days')
  ) + 1
);

Upvotes: 19

Sumit
Sumit

Reputation: 653

Try this Using moment.js (Its quite easy to compute date operations in javascript)

firstDate.diff(secondDate, 'days', false);// true|false for fraction value

Result will give you number of days in integer.

Upvotes: 23

David
David

Reputation: 1591

Also you can use this code: moment("yourDateHere", "YYYY-MM-DD").fromNow(). This will calculate the difference between today and your provided date.

Upvotes: 5

Rakesh Prajapati
Rakesh Prajapati

Reputation: 1104

If you are using moment.js you can do it easily.

var start = moment("2018-03-10", "YYYY-MM-DD");
var end = moment("2018-03-15", "YYYY-MM-DD");

//Difference in number of days
moment.duration(start.diff(end)).asDays();

//Difference in number of weeks
moment.duration(start.diff(end)).asWeeks();

If you want to find difference between a given date and current date in number of days (ignoring time), make sure to remove time from moment object of current date as below

moment().startOf('day')

To find difference between a given date and current date in number of days

var given = moment("2018-03-10", "YYYY-MM-DD");
var current = moment().startOf('day');

//Difference in number of days
moment.duration(given.diff(current)).asDays();

Upvotes: 91

Supr
Supr

Reputation: 19022

http://momentjs.com/ or https://date-fns.org/

From Moment docs:

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days')   // =1

or to include the start:

a.diff(b, 'days')+1   // =2

Beats messing with timestamps and time zones manually.

Depending on your specific use case, you can either

  1. Use a/b.startOf('day') and/or a/b.endOf('day') to force the diff to be inclusive or exclusive at the "ends" (as suggested by @kotpal in the comments).
  2. Set third argument true to get a floating point diff which you can then Math.floor, Math.ceil or Math.round as needed.
  3. Option 2 can also be accomplished by getting 'seconds' instead of 'days' and then dividing by 24*60*60.

Upvotes: 912

Richard
Richard

Reputation: 1158

Try:

//Difference in days

var diff =  Math.floor(( start - end ) / 86400000);
alert(diff);

Upvotes: 20

Related Questions