9

I am having a problem with the DateDiff function. I am trying to figure out the Difference between two dates/times. I have read this posting (What's the best way to calculate date difference in Javascript) and I also looked at this tutorial (http://www.javascriptkit.com/javatutors/datedifference.shtml) but I can't seem to get it.

Here is what I tried to get to work with no success. Could someone please tell me what I am doing and how I can simplify this. Seems a little over coded...?

//Set the two dates
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currDate = month + "/" + day + "/" + year;
var iniremDate = "8/10/2012";

//Show the dates subtracted
document.write('DateDiff is: ' + currDate - iniremDate);

//Try this function...
function DateDiff(date1, date2) {
    return date1.getTime() - date2.getTime();
}

//Print the results of DateDiff
document.write (DateDiff(iniremDate, currDate);
shA.t
  • 16,580
  • 5
  • 54
  • 111
Frank G.
  • 1,519
  • 7
  • 25
  • 43

4 Answers4

13

Okay for those who would like a working example here is a simple DateDiff ex that tells date diff by day in a negative value (date passed already) or positive (date is coming).

EDIT: I updated this script so it will do the leg work for you and convert the results in to in this case a -10 which means the date has passed. Input your own dates for currDate and iniPastedDate and you should be good to go!!

//Set the two dates
var currentTime   = new Date()
var currDate      = currentTime.getMonth() + 1 + "/" + currentTime.getDate() + "/" + currentTime.getFullYear() //Todays Date - implement your own date here.
var iniPastedDate = "8/7/2012" //PassedDate - Implement your own date here.

//currDate = 8/17/12 and iniPastedDate = 8/7/12

function DateDiff(date1, date2) {
    var datediff = date1.getTime() - date2.getTime(); //store the getTime diff - or +
    return (datediff / (24*60*60*1000)); //Convert values to -/+ days and return value      
}

//Write out the returning value should be using this example equal -10 which means 
//it has passed by ten days. If its positive the date is coming +10.    
document.write (DateDiff(new Date(iniPastedDate),new Date(currDate))); //Print the results...
Tony Basallo
  • 3,000
  • 2
  • 29
  • 47
Frank G.
  • 1,519
  • 7
  • 25
  • 43
  • 1
    Note that this is quite limited - `getDay` returns the day of the week so it's basically only usable if both dates are in the same week (`iniremDate = "8/8/2012"` would yield the same result). – pimvdb Aug 17 '12 at 10:25
  • @pimvdb what if you use getTime then does that work for any date? – Frank G. Aug 17 '12 at 10:30
  • 1
    `getTime` is not relative to the current week or something like that; it gives a unique number for each date so it's reliable for your needs. The only problem is that you'd need to convert the milliseconds difference into a days difference yourself. – pimvdb Aug 17 '12 at 10:32
  • I have to say it seems like every language doesn't ever have a good DateDiff and as important of a function as it is you would think they would have this covered for you... make life a little easier! – Frank G. Aug 17 '12 at 10:37
  • @pimvdb I updated the script now so it would do the leg work for people. Thanks for you help with this!! :) – Frank G. Aug 17 '12 at 10:55
  • @pimvdb well maybe this will help someone looking for it! It will at least point them in the right direction. – Frank G. Aug 17 '12 at 10:57
7

Your first try does addition first and then subtraction. You cannot subtract strings anyway, so that yields NaN.

The second trry has no closing ). Apart from that, you're calling getTime on strings. You'd need to use new Date(...).getTime(). Note that you get the result in milliseconds when subtracting dates. You could format that by taking out full days/hours/etc.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • As per you said it would show a negative value. Is this correct it seems correct because it shows the negative values. document.write(new Date(iniremDate)-new Date(currDate)); function DateDiff(date1, date2) { return date1.getTime() - date2.getTime(); } document.write (DateDiff(new Date(iniremDate),new Date(currDate))); – Frank G. Aug 17 '12 at 10:15
  • Since `iniremDate` is 10th of August and it's the 17th right now, you obviously get a negative value. If you want to subtract the opposite, just subtract `date1` from `date2`. – pimvdb Aug 17 '12 at 10:16
  • The returning value is -604800000 I take it that means it has passed that long ago in seconds? – Frank G. Aug 17 '12 at 10:16
  • 2
    @Frank G: That's in milliseconds. If you convert it to days it's indeed 7 days ago: `-604800000 / (24*60*60*1000) === -7`. – pimvdb Aug 17 '12 at 10:17
  • Yes, your correct thank you I was able to get it working thanks for the help. I posted a full working example below for others who might be looking for help with this to!!! Thanks :D – Frank G. Aug 17 '12 at 10:23
2
function setDateWeek(setDay){
    var d = new Date();
    d.setDate(d.getDate() - setDay); // <-- add this
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    return curr_date + "-" + curr_month + "-" + curr_year;
}


setDateWeek(1);
AKHIL
  • 135
  • 3
-3

No need to include JQuery or any other third party library.

Specify your input date format in title tag.

HTML:

< script type="text/javascript" src="http://services.iperfect.net/js/IP_generalLib.js">

Use javascript function:

IP_dateDiff(strDate1,strDate2,strDateFormat,debug[true/false])

alert(IP_dateDiff('11-12-2014','12-12-2014','DD-MM-YYYY',false));

IP_dateDiff function will return number of days.

shA.t
  • 16,580
  • 5
  • 54
  • 111