Javascript 日期差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12003660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript DateDiff
提问by Frank G.
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.
我的 DateDiff 函数有问题。我想弄清楚两个日期/时间之间的差异。我已经阅读了这篇文章(在 Javascript 中计算日期差异的最佳方法是什么),我也看过这个教程(http://www.javascriptkit.com/javatutors/datedifference.shtml),但我似乎无法理解.
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);
采纳答案by pimvdb
Your first try does addition first and then subtraction. You cannot subtract strings anyway, so that yields NaN
.
您的第一次尝试是先加法,然后是减法。无论如何,您不能减去字符串,因此会产生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.
第二次尝试没有结束)
。除此之外,您正在调用getTime
字符串。你需要使用new Date(...).getTime()
. 请注意,减去日期时,您会得到以毫秒为单位的结果。您可以通过取出整天/小时/等来格式化它。
回答by Frank G.
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).
好的,对于那些想要一个工作示例的人来说,这里是一个简单的 DateDiff ex,它以负值(日期已经过去)或正值(日期即将到来)按天告诉日期差异。
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!!
编辑:我更新了这个脚本,所以它会为你做腿部工作,并将结果转换为 -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...
回答by AKHIL
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);
回答by Neeraj Dhekale
No need to include JQuery or any other third party library.
无需包含 JQuery 或任何其他第三方库。
Specify your input date format in title tag.
在标题标签中指定您的输入日期格式。
HTML:
HTML:
< script type="text/javascript" src="http://services.iperfect.net/js/IP_generalLib.js">
Use javascript function:
使用javascript函数:
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.
IP_dateDiff 函数将返回天数。