Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

javascript - Calculating the difference between two dates

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...