2012-08-17 43 views
5

我遇到了DateDiff函数的问题。我试图找出两个日期/时间之间的差异。我已阅读这篇文章(What's the best way to calculate date difference in Javascript),我也看了这个教程(http://www.javascriptkit.com/javatutors/datedifference.shtml),但我似乎无法得到它。Javascript DateDiff

下面是我试图去工作没有成功。有人可以告诉我我在做什么以及如何简化这一点。似乎有点过分编码...?

//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); 

回答

5

您的第一次尝试先添加,然后减去。无论如何,你不能减去字符串,这样收益率为NaN

第二个trry没有关闭)。除此之外,你打电话给getTime。你需要使用new Date(...).getTime()。请注意,您在减去日期时以毫秒为单位获得结果。你可以通过完整的日期/时间等来设置格式。

+0

正如你所说,它会显示一个负值。这是否正确,因为它显示了负值。 \t \t document.write(new Date(iniremDate)-new Date(currDate)); \t \t \t 功能\t DateDiff的(DATE1,DATE2){ \t \t返回date1.getTime() - date2.getTime(); \t \t} \t \t \t \t 文件撰写\t(DateDiff的(新日期(iniremDate),新的日期(currDate))); – 2012-08-17 10:15:06

+0

因为'iniremDate'是8月10日,现在是17日,你显然会得到一个负值。如果你想减去相反的,只需从'date2'减去'date1'。 – pimvdb 2012-08-17 10:16:46

+0

返回值是-604800000我认为这意味着它已经很久以前在几秒钟内通过了? – 2012-08-17 10:16:46

9

好为那些想工作的例子谁在这里是一个简单的则DateDiff前,在负值(日期已经过去了)或正一天天告诉日期差异(日期快到了)。

编辑:我更新了这个脚本,所以它会为你做腿部的工作,结果在这种情况下-10这意味着该日期已过转换中。输入你自己的日期currDate和iniPastedDate,你应该很好去!

//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... 
+1

请注意,这是相当有限的 - 'getDay'返回星期几,所以它基本上只有在两个日期都在同一周内才可用('iniremDate =“2012/8/8”'会产生相同的结果)。 – pimvdb 2012-08-17 10:25:59

+0

@pimvdb如果你使用getTime,那么这对任何日期都有效吗? – 2012-08-17 10:30:17

+1

'getTime'不是相对于当前周或类似的东西;它为每个日期提供了一个唯一的编号,因此它对您的需求是可靠的。唯一的问题是你需要将毫秒差异转换成你自己的天差。 – pimvdb 2012-08-17 10:32:56

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); 
0

无需包括JQuery的或其他任何第三方库。

在标题标签中指定输入日期格式。

HTML:

<脚本类型= “文本/ JavaScript的” SRC = “http://services.iperfect.net/js/IP_generalLib.js”>

使用JavaScript功能:

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

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

IP_dateDiff函数将返回天数。