5

I have tried like this

<script type="text/javascript">
     var date2=02/09/2009;
     var date1=03/12/2009;
     var diff = date1.getDate()-date2.getDate();
     alert (diff);

</script>

but it's not working, is that reason getDate will work only for Date?

How to find the difference between these two dates? Am I not able use this function, because I am adding javascript in salesforce CRM apex pages?

Edit 1 : infact this too not working

<script type="text/javascript">
     var date2= new Date ("02/09/2009");
     var date1= new Date ("04/09/2009");
     var diff = date1.getDate()-date2.getDate();
     alert (diff);

</script>

Edit 2 : its not working too ...

<script type="text/javascript">
     var date2= "02/09/2009";
     var date1= "04/09/2009";
     var diff2 =    new Date(Date.parse("03/12/2009")-
                        Date.parse("02/09/2009")).toLocaleDateString();

// var new_date = new Date (1970, 01, 01); // var diff3 = diff2.getDate(); alert (diff2);

</script>
joe
  • 34,529
  • 29
  • 100
  • 137
  • Thanks every one , i just answer the answer for if the user is entering like this formation , how i can find the difference ? – joe Sep 11 '09 at 11:35
  • 2
    did anyone here suggested to use `new Date('02/09/2009')`? why would you expect it to work? – SilentGhost Sep 11 '09 at 11:39
  • But our client required and give like this ... So i need the solution for that :-( – joe Sep 11 '09 at 12:37
  • 1
    Then parse the string to extract the values from the string as numbers. Hint: Look into the functions string.split() and and parseInt() for doing that. – Residuum Sep 11 '09 at 13:09
  • @krish - i just updated my ans.. just look into that... it may work. it worked for me.. – RameshVel Sep 11 '09 at 14:16

10 Answers10

17

<script language="JavaScript">
<!--
function dstrToUTC(ds) {
 var dsarr = ds.split("/");
 var mm = parseInt(dsarr[0],10);
 var dd = parseInt(dsarr[1],10);
 var yy = parseInt(dsarr[2],10);
 return Date.UTC(yy,mm-1,dd,0,0,0);
}

function datediff(ds1,ds2) {
 var d1 = dstrToUTC(ds1);
 var d2 = dstrToUTC(ds2);
 var oneday = 86400000;
 return (d2-d1) / oneday;
}

// test cases are below

var a; var b;

a = "01/09/1999";
b = "01/10/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/12/1999";
b = "01/19/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/19/1999";
b = "01/12/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/03/1999";
b = "01/13/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "04/30/1999";
b = "05/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "05/30/1999";
b = "06/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "02/28/1999";
b = "03/01/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "02/28/2000";
b = "03/01/2000";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/01/1999";
b = "12/31/1999";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "01/01/2000";
b = "12/31/2000";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

a = "12/15/1999";
b = "01/15/2001";
document.write("From "+a+" to "+b+" is "+datediff(a,b)+" day(s)<br>");

// -->
</script>
Robert L
  • 1,963
  • 2
  • 13
  • 11
4

date2 = 02/09/2009 is not considered to be a date. it works this way. First it devides 02/09, it returns 0.2222222222222222 and its been divided by 2009 (0.2222222222222222/2009). finally you got an result date2 = 0.0001106133510314695. same way it calculates the result for date1.

this is not a valid operation. If you wanted to define the date. make sure that, you placed the data in a right date format.

use either new Date() or Date.parse("02/09/2009")

Edit:

   new Date(Date.parse("03/12/2009")-Date.parse("02/09/2009")).toLocaleDateString() Or
   new Date(date1- date2).toLocaleDateString()

isnt that work..??

Edit :

may be this will work.. can u try this..

 Date.parse("03/12/2009")-Date.parse("02/09/2009") / (24*60*60*1000) 

and it returns 31 days

it seems working for me.. but in my time zone it took 03/12/2009 as 3rd month 11th day and year 2009

(24*60*60*1000) = Number of milliseconds per day

RameshVel
  • 64,778
  • 30
  • 169
  • 213
2

getDate is a method of Date object. as any docs clearly state it returns the day of the month in range 0 to 31. it wouldn't make sense to try to subtract one from the other if it's not the same month.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
1
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

alert(weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds");
bogdanbrudiu
  • 534
  • 9
  • 32
  • Don't you think it's a bit redundant that you're setting the time difference to another date object? date1.getTime() - date2.getTime() would return the difference in milliseconds anyway. – Andy E Sep 17 '09 at 08:30
1
  1. getDate() returns the day of the month, in your case: 9 and 12.
  2. You should explicitely define date objects via the following. Months are 0-based, therefore 0 = January, 1 = February ...

    var date2 = new Date(2009, 1, 9);

  3. These are numbers, not a string. When you want to use a string for the date, then you need exactly that format, nothing is optional:

    var newDate = new Date("month day, year hours:minutes:seconds"); var date2 = new Date("february 9, 2009 00:00:00");

[Edit] Complete solution, if dates are in format mm/dd/yyyy, and the difference should be in days:

<script type="text/javascript">
    function daysFromString(dateString)
    {
        // split strings at / and return array
        var splittedString = dateString.split("/");
        // make a new date. Caveat: Months are 0-based in JS
        var newDate = new Date(parseInt(splittedString[2], 10), parseInt(splittedString[0], 10)-1, parseInt(splittedString[1], 10));
        // returns days since jan 1 1970
        return Math.round(newDate.getTime() / (24*3600*1000));
    }

    var dateString2 = "02/09/2009";
    var dateString1= "03/12/2009";
    var dateDays1 = daysFromString(dateString1);
    var dateDays2 = daysFromString(dateString2);
    var diff = dateDays1 - dateDays2;
    alert (diff);
</script>
Residuum
  • 11,878
  • 7
  • 40
  • 70
1

use Date.parse(date1) - Date.parse(date2)

Khodor
  • 996
  • 1
  • 6
  • 13
0

Here is explained how to find the difference between two dates in javascript

dmateev
  • 64
  • 2
  • 11
0

yes. You must use Date object, like this

var d = new Date(2009,9,19); //19th october, months are 0-based
var d2 = new Date(2009,10,12);
var diff = (d2 - d)/60000;  //diff in minutes
alert(diff / 24);   //difference in hours
Pier Luigi
  • 7,871
  • 9
  • 36
  • 46
0

Try

var diff2 =    Date.parse("03/12/2009") - Date.parse("02/09/2009");

It will give you the difference between the dates in milliseconds. Divide this number by 86,400,000 and the difference will be in days.

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
-1
<script type="text/javascript">
function GetD()
{
    var date1 = new Date ( 2009 , 09 , 02 );
    var date2 = new Date ( 2009 , 12 , 03 );        

    var diff = days_between (date1,date2);
    alert ( diff );
}
function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);

    // Convert back to days and return

    return Math.round(difference_ms/ONE_DAY);
}
</script>
rahul
  • 184,426
  • 49
  • 232
  • 263