5

I want to do same thing as How do I get the number of days between two dates in JavaScript?

but I want do the same on this date format: 2000-12-31.

Community
  • 1
  • 1
DAKSH
  • 460
  • 1
  • 5
  • 22

4 Answers4

13
function daysBetween(date1String, date2String){
  var d1 = new Date(date1String);
  var d2 = new Date(date2String);
  return (d2-d1)/(1000*3600*24);
}

console.log( daysBetween('2000-12-31', '2005-05-04') );  //-> 1585

ISO8601 date strings are recognized by JavaScript directly. No need to parse them yourself.

Joe
  • 80,724
  • 18
  • 127
  • 145
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 2
    +1 Great answer. Too many people are either unaware of or just plain ignore javascript's Date object. No need for external libraries most of the time. – Endophage Jun 27 '11 at 19:14
  • @naveen A good point on `Math.floor` (lowercase "f"), but I intentionally allowed the method to return a non-integer number of days in case the OP wanted to [`floor`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/floor), [`round`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/round), or [`ceil`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/ceil) the result. It's not clear what use case is at hand, and any/all of those methods can be appropriate for this under different circumstances. – Phrogz Jun 27 '11 at 19:35
  • yep. lower case. too much c#.. :) very valid point. but i dont have a vaild case for round. – naveen Jun 27 '11 at 19:41
  • 1
    @naveen No use case for `round`? How about daylight savings time? `daysBetween('2005-01-1', '2005-08-1') -> 211.95833` versus `daysBetween('2005-11-1', '2005-12-1') -> 30.041667`. – Phrogz Oct 06 '11 at 22:07
  • very valid point. as i live at a country with daylight savings i tend to forget that. – naveen Oct 07 '11 at 07:44
2

Try this.

var toDate = "2000-12-31";
var fromDate = "2000-10-30";
var diff =  Math.floor(( Date.parse(toDate) - Date.parse(fromDate) ) / 86400000);

You wont be asking this question if you have checked the answer with more up-votes and not the marked answer on the link you have provided. :)

naveen
  • 53,448
  • 46
  • 161
  • 251
1

Well, it's not jQuery, but just as easy to work with. Check out DateJS. Can parse dates, differences, and more.

Sean O
  • 2,276
  • 1
  • 21
  • 24
0

The other solutions here do not take into account the TimeZone information (which is fine if that is what you want) but I came across a problem like this:
I have two dates: Thu Mar 01 2012 00:00:00 GMT+0100 and Sat Mar 31 2012 00:00:00 GMT+0200 which will give you 29.95833 days before the Math.floor and hence 29 days after the floor. This is not the 30 days I was expecting. Clearly Daylight Saving has kicked in and shortened one of the days by an hour.

Here is my solution which takes TimeZones into account:

function daysBetween(date1String, date2String) {
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var ONE_MINUTE = 1000 * 60;

    var d1 = new Date(date1String);
    var d2 = new Date(date2String);
    var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
    var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;

    return Math.floor(d1_ms - d2_ms/ONE_DAY);
}
Steve
  • 1,769
  • 2
  • 22
  • 33