我试过这样的
<script type="text/javascript">
var date2=02/09/2009;
var date1=03/12/2009;
var diff = date1.getDate()-date2.getDate();
alert (diff);
</script>
但它没有用,是getDate
仅适用于Date
的原因吗?
如何找到这两个日期之间的差异?我无法使用此功能,因为我在salesforce CRM apex页面中添加了javascript?
编辑1:事实上,这也无法正常工作
<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>
编辑2:它也不起作用......
<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>
答案 0 :(得分: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>
答案 1 :(得分:4)
date2 = 02/09/2009不被视为日期。它以这种方式工作。首先它是02/09,它返回0.2222222222222222并且除以2009(0.2222222222222222/2009)。最后你得到了一个结果date2 = 0.0001106133510314695。以同样的方式计算date1的结果。
这不是有效的操作。如果要定义日期。确保您以正确的日期格式放置数据。
使用新的Date()或Date.parse(“02/09/2009”)
修改强>
new Date(Date.parse("03/12/2009")-Date.parse("02/09/2009")).toLocaleDateString() Or
new Date(date1- date2).toLocaleDateString()
不起作用.. ??
修改:
可能会有效..你能不能试试..
Date.parse("03/12/2009")-Date.parse("02/09/2009") / (24*60*60*1000)
它返回31天
它似乎对我有用..但在我的时区它花了03/12/2009作为第3个月的第11天和2009年
(24 * 60 * 60 * 1000)=每天的毫秒数
答案 2 :(得分:2)
getDate
是Date
对象的方法。因为任何文档明确说明它返回0到31范围内的月份日期。如果不是同一个月,尝试从另一个中减去一个是没有意义的。
答案 3 :(得分: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");
答案 4 :(得分:1)
您应该通过以下方式明确定义日期对象。月份为0,因此0 = 1月,1 = 2月......
var date2 = new Date(2009,1,9);
这些是数字,而不是字符串。如果您想使用字符串作为日期,那么您需要完全符合该格式,没有任何内容是可选的:
var newDate = new Date(“month day,year hours:minutes:seconds”); var date2 = new Date(“2009年2月9日00:00:00”);
<强> [编辑] 强> 完整的解决方案,如果日期格式为mm / dd / yyyy,差异应以天为单位:
<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>
答案 5 :(得分:1)
使用Date.parse(date1) - Date.parse(date2)
答案 6 :(得分:0)
Here解释了如何在javascript中找到两个日期之间的差异
答案 7 :(得分:0)
是肯定的。 您必须使用日期对象,例如
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
答案 8 :(得分:0)
尝试
var diff2 = Date.parse("03/12/2009") - Date.parse("02/09/2009");
它会以毫秒为单位显示日期之间的差异。将此数字除以86,400,000,差异将以天为单位。
答案 9 :(得分:-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>