-1

Today is 3/26/2016. I want to Print out how many days are until 4/05/2016 ex.

The answer is 10 days. I want to do this with Javascript Code.

With standard javascript or with the use of a Library or Framework. Just to calculate the Difference between two dates. Easy, no too complicated.

InsertDateHere ------- 3/26/2016

InsertDateHere ------ 4/05/2016

ResultHere -------------- AnswerHere ("10" ex.)

if anyone can help, please Do. Thanks a lot People!

1 Answers1

1

Moment.js is a helpful library that can also handle timezones and DST. The method that you're looking for is difference.

If this is the only date-handling that you'll be doing, it's likely not worth it to pull in a library. You can handle this easily with a simple function.

var diffInDays = function diffInDays (dateA, dateB) {
  var difference = dateA - dateB;
  return Math.floor(Math.abs(difference / (1000*60*60*24)));
};

diffInDays(Date.parse('3/26/2016'), Date.parse('4/05/2016'));
// > 10
quexxon
  • 11
  • 2
  • Note that it is strongly recommended **not** to use the Date constructor (or Date.parse) to parse strings. If using a parser (and moment.js has a good one) then the format should always be specified. – RobG Mar 28 '16 at 02:18