1

I am getting two different dates. How can I get difference between two dates? Dates are in d.m.y format. eg: 11.10.18 & 13.10.18

var dateOne   = new Date('11.10.18');
console.log(dateOne);
var dateTwo   = new Date('13.10.18');
var timeDiff  = Math.abs(dateTwo.getTime() - dateOne.getTime());
var diffDays  = Math.ceil(timeDiff / (1000 * 3600 * 24));

On Chrome console, result is correct.

Sat Nov 10 2018 00:00:00 GMT+0100 (Central European Standard Time)

On Firefox console, error is getting.

Invalid Date

Any help would be appreciated Thanks.

Sarath TS
  • 2,432
  • 6
  • 32
  • 79
  • 3
    The Chrome result isn't correct, since 11.10. is Oct 11, not Nov 10. –  Oct 03 '18 at 09:44
  • 2
    Please search thoroughly before posting. More on searching [here](/help/searching). **Every** possible question about parsing dates with JavaScript has been repeated asked and answered. ;-) Examples: [1](https://stackoverflow.com/questions/31876348/), [2](https://stackoverflow.com/questions/37776970/javascript-convert-string-to-date-time) The `Date` constructor isn't specified to parse that format; you have to parse it yourself (with your own code, or a library). – T.J. Crowder Oct 03 '18 at 09:44
  • @ChrisG Thank you. I updated my question. – Sarath TS Oct 03 '18 at 09:45
  • 2
    @samsam Changing the console result you copy pasted here is nonsense, my point is that Chrome *does not parse a German date correctly, as evident by the console output*, so you need to parse it manually anyway. Chrome parses the second date as `Invalid Date` because it assumes 13.10. is the tenth day of the 13th month. –  Oct 03 '18 at 09:47
  • It is because firefox is taking dates as MM/DD/YYYY and giving error on 13/10/2018 – Amit chauhan Oct 03 '18 at 09:48
  • @Amitchauhan, Thank you. Any library or built in function for JavaScript to convert this? – Sarath TS Oct 03 '18 at 09:49
  • 1
    It's similar to second case in [this answer](https://stackoverflow.com/a/33299764/7404943), just change separator to `.` and change year to long format – barbsan Oct 03 '18 at 09:52
  • @Samsam I've Updated my answer and you do not require any external Js for the conversion – Amit chauhan Oct 03 '18 at 10:02
  • @Amitchauhan, Thank you. But input year is `18` not `2018`. Getting dates like `var dateOne = 11.10.18`; `var dateTwo = 13.10.18`; – Sarath TS Oct 03 '18 at 10:12
  • @Samsam 11.10.18 also working, see my updated answer and run the code snippet – Amit chauhan Oct 03 '18 at 10:14

1 Answers1

0

This is working perfectly in chrome and giving result as expected Make your date as in the format of MM/dd/YYYY before passing to date constructor

var dateOneString = "11.10.18";
var dateTwoString = "13.10.18";
var dateOneSplitted = dateOneString.split('.');
var dateTwoSplitted = dateTwoString.split('.');
var dateOne = new Date("20"+dateOneSplitted[2], dateOneSplitted[1]-1, dateOneSplitted[0]);
var dateTwo = new Date("20"+dateTwoSplitted[2], dateTwoSplitted[1]-1, dateTwoSplitted[0]);

var timeDiff = Math.abs(dateTwo.getTime() - dateOne.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log("Days Difference - "+diffDays);
console.log("Date One - "+dateOne);
console.log("Date Two - "+dateTwo);
Amit chauhan
  • 540
  • 9
  • 22