我有两个日期为dd / mm / yyyy格式。如何在javascript / jquery中计算这两个日期之间的天数。
示例:Fom日期是20/06/2000,迄今为止是2011年8月16日
答案 0 :(得分:6)
简单代码
var Date1 = new Date (2008, 7, 25);
var Date2 = new Date (2009, 0, 12);
var Days = Math.floor((Date2.getTime() - Date1.getTime())/(1000*60*60*24));
答案 1 :(得分:3)
var date1 = new Date(2000, 6, 20);
var date2 = new Date(2011, 8, 16);
var one_day = 1000*60*60*24; //Get 1 day in milliseconds
var days = Math.ceil( (date2.getTime() - date1.getTime() ) / one_day);
Math.ceil
向上舍入,Math.floor
向下舍入。
http://www.javascriptkit.com/javatutors/datedifference.shtml
答案 2 :(得分:3)
t1="10/10/2006";
t2="15/10/2006";
//Total time for one day
var one_day=1000*60*60*24; //Here we need to split the inputed dates to convert them into standard format for further execution
var x=t1.split("/");
var y=t2.split("/"); //date format(Fullyear,month,date)
var date1=new Date(x[2],(x[1]-1),x[0]);
// it is not coded by me,but it works correctly,it may be useful to all
var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;
//Calculate difference between the two dates, and convert to days
_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));