Hi,

I want to calculate the number of days between two dates.

Date is in the below format:
First Date : 2010-09-27 05:00:00
Second Date : 2010-10-1 08:00:00

Thanks.

Edited by techie929: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Airshow
``````//Set the two dates
today=new Date()
var christmas=new Date(today.getFullYear(), 11, 25) //Month is 0-11 in JavaScript
if (today.getMonth()==11 && today.getDate()>25) //if Christmas has passed already
christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas
//Set 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
document.write(Math.ceil((christmas.getTime()-today.getTime())/(one_day))+
" days left until Christmas!")``````

Techie,

First extend `Date` to add a `setDateTime` method, which accepts a string of the necessary format :

``````Date.prototype.setDateTime = function(dt, ignoreTime) {
try {
ignoreTime = (!ignoreTime) ? false : true;
dt = dt.replace(/\s+/g, ' ').replace(/^\s/, '').replace(/\s\$/, '');//reduce multiple spaces to single space, then purge any leading and trailing spaces
var parts = dt.split(' ');
var d = parts[0].split('-');
if(d.length >= 1) { this.setYear(d[0]); }
if(d.length >= 2) { this.setMonth(d[1]-1); }
if(d.length >= 3) { this.setDate(d[2]); }
var t = (ignoreTime || parts.length < 2) ? [0,0,0] : parts[1].split(':');
if(t.length >= 1) { this.setHours(t[0]); }
if(t.length >= 2) { this.setMinutes(t[1]); }
if(t.length >= 3) { this.setSeconds(t[2]); }
return this.getTime();
}
catch(e) { return false; }
};``````

This includes a measure of inbuilt safety to help guard against an error should an incomplete string, or a string of the wrong format, be passed to it.

Now you can use this method as follows :

``````var d1 = new Date();
var d2 = new Date();
var first_Date  = '2010-09-27 05:00:00';
var second_Date = '2010-10-1 08:00:00';
d1.setDateTime(first_Date, false);
d2.setDateTime(second_Date, false);``````

Depending on exactly what you are trying to do, you may choose to set the second parameter to true, which would cause the hh:mm:ss part of the dateTime to be ignored (effectively setting the time to midnight).

To calculate the difference between two Dates, we add a second method to the Date prototype:

``````Date.prototype.subtract = function(d, returnAs) {
returnAs = (!returnAs) ? '' : returnAs;
switch(returnAs) {
case 'days':
return (this-d)/(24*60*60*1000);
break;
case 'hours':
return (this-d)/(60*60*1000);
break;
case 'minutes':
return (this-d)/(60*1000);
break;
case 'seconds':
return (this-d)/1000;
break;
default://milliseconds
return this-d;
}
}``````

Call as follows:

``var days = d2.subtract(d1, 'days');``

The result is a floating point number, which you can use raw or as `Math.floor(days)` , `Math.ceil(days)` or `Math.round(days)` as appropriate to your application.

This gives some reusable code with a degree of flexibility.

Airshow

Edited by Airshow: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.