-1

How to find difference between two dates displayed in two text boxes ?.I was using following code and what i want is if the date difference goes beyond 90 days then i have to show some message to user.Here dates are dd/MM/yyyy format ,so here if i select from date as 23/8/17 and todate as 9/11/2017 ,the difference is coming as 423 days why?

function checkDate() {
            debugger;
            var txtdate1 = document.getElementById('<%= txtFromDate.ClientID %>').value;
            var txtdate2 = document.getElementById('<%= txtToDate.ClientID %>').value;
            var date1 = new Date(txtdate2 );
            var date2 = new Date(txtdate1);
            var timeDiff = Math.abs(date2.getTime() - date1.getTime());
            var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
            if (diffDays > 90) {
                var Message = 'Dates are too far apart,It should be with in 3 months';}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
peter
  • 8,158
  • 21
  • 66
  • 119
  • Could [this](https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) be of help? – Simon Wilson Dec 09 '17 at 19:40
  • Your code is JavaScript, there is nothing C# or ASP.NET related really, so please tag correctly and I’m sure there is a question about calculating date differences with JavaScript – Sami Kuhmonen Dec 09 '17 at 19:43
  • Your JavaScript solution is taking the difference between ticks (milliseconds since Epoch) then computing days from total msec. You can do it with the same basic algorithm in C# but the ticks are nanoseconds (the Epoch is different as well but that won't matter here). However, the linked answer is the easiest method. – Jasen Dec 09 '17 at 19:49
  • @Simon Wilson i dont want unncesseray post back rather in javascript – peter Dec 09 '17 at 19:50
  • @jasen i dont want unncesseray post back rather in javascript – peter Dec 09 '17 at 19:51
  • Does your browser recognize dd/MM/yyyy as a standard format for your locale? – Jasen Dec 09 '17 at 19:52

1 Answers1

0

The result is 19 .I think your formating for date is wrong :

Math.ceil(Math.abs((new Date('2017/08/23 00:00:00.000')).getTime()-(new Date('2017/09/11 00:00:00.000')).getTime())/(1000*3600*24))

//==> 19
nAviD
  • 2,784
  • 1
  • 33
  • 54