0

I want to calculate duration between today's date and a project start day.For example today is 29-Jun-2016 and my project will be started from 1 July then the duration would be 3 days.How to solve this?

function calculateBackDay(){
  var MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
  var leaveStartDateValue = $('#leave_start_date').val();
  var leaveStartDate = new Date(leaveStartDateValue);
  var current = new Date();
  var month = current.getMonth()+1;
  var day = current.getDate();
  var currentDateValue = ((''+day).length<2 ? '0' : '') + day + '-' + ((''+month).length<2 ? '0' : '') + month + '-' + current.getFullYear();
  var currentDate = new Date(currentDateValue);
  var totalBackDays = Math.floor((leaveStartDate.getTime() - currentDate.getTime()) / MILLISECONDS_PER_DAY) + 1;
  alert(totalBackDays);
}

N.B: alert shown NaN..

McNets
  • 10,352
  • 3
  • 32
  • 61
M.Topa
  • 64
  • 1
  • 10
  • What's `var currentDate = new Date(currentDateValue);` for when you already have a `var current = new Date();` – progrAmmar Jun 29 '16 at 08:28
  • What is the result of `var totalBackDays = Math.floor((leaveStartDate - current) / MILLISECONDS_PER_DAY) + 1;` – progrAmmar Jun 29 '16 at 08:30
  • Please have a look at [this](http://stackoverflow.com/a/2609579/6527256) answer. You can do Math.floor(days); – JK12321 Jun 29 '16 at 08:35
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Matt Raines Jun 29 '16 at 09:21
  • Matt Raines..get the number of days between two dates and distance between today and another day is not same question..you should re-read the question. – M.Topa Jun 29 '16 at 15:50

0 Answers0