JavaScript中的date之间的差异

如何find两个date之间的差异?

通过使用Date对象及其毫秒值,可以计算差异:

var a = new Date(); // Current date now. var b = new Date(2010, 0, 1, 0, 0, 0, 0); // Start of 2010. var d = (ba); // Difference in milliseconds. 

您可以通过将毫秒除以1000来得到秒数(以整数/整数),将其转换为秒,然后将结果转换为整数(这将删除表示毫秒的小数部分):

 var seconds = parseInt((ba)/1000); 

然后,您可以通过将seconds除以60并将其转换为整数来获得整个minutes ,然后将minutes除以60并将其转换为整数,然后以相同方式将其转换为更长的时间单位。 由此,可以创build下部单元的值和下部单元的值中的最大时间单位的函数:

 function get_whole_values(base_value, time_fractions) { time_data = [base_value]; for (i = 0; i < time_fractions.length; i++) { time_data.push(parseInt(time_data[i]/time_fractions[i])); time_data[i] = time_data[i] % time_fractions[i]; }; return time_data; }; // Input parameters below: base value of 72000 milliseconds, time fractions are // 1000 (amount of milliseconds in a second) and 60 (amount of seconds in a minute). console.log(get_whole_values(72000, [1000, 60])); // -> [0,12,1] # 0 whole milliseconds, 12 whole seconds, 1 whole minute. 

如果您想知道上面为第二个Date对象提供的input参数是什么,请参阅下面的名称:

 new Date(<year>, <month>, <day>, <hours>, <minutes>, <seconds>, <milliseconds>); 

正如在这个解决scheme的评论中所指出的那样,你不一定需要提供所有这些值,除非它们是你想要表示的date所必需的。

使用DateJs

瑞士刀工具的JavaScriptdate时间编程!

http://www.datejs.com/

我已经find了这个,它适用于我:

计算两个已知date之间的差异

不幸的是,计算两个已知date之间的date间隔(如天,周或月)并不容易,因为您不能只将Date对象添加在一起。 为了在任何计算中使用Date对象,我们必须首先检索Date的内部毫秒值,该值存储为一个大整数。 做这个的函数是Date.getTime()。 一旦两个date都被转换,从前面的date减去后面的date将返回毫秒的差值。 然后可以通过将该数字除以相应的毫秒数来确定期望的间隔。 例如,要获得给定毫秒数的天数,我们除以86,400,000,一天中的毫秒数(1000 x 60秒x 60分钟x 24小时):

 Date.daysBetween = function( date1, date2 ) { //Get 1 day in milliseconds 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 = date2_ms - date1_ms; // Convert back to days and return return Math.round(difference_ms/one_day); } //Set the two dates var y2k = new Date(2000, 0, 1); var Jan1st2010 = new Date(y2k.getFullYear() + 10, y2k.getMonth(), y2k.getDate()); var today= new Date(); //displays 726 console.log( 'Days since ' + Jan1st2010.toLocaleDateString() + ': ' + Date.daysBetween(Jan1st2010, today)); 

舍入是可选的,取决于你是否想要部分日子。

参考

  // This is for first date first = new Date(2010, 03, 08, 15, 30, 10); // Get the first date epoch object document.write((first.getTime())/1000); // get the actual epoch values second = new Date(2012, 03, 08, 15, 30, 10); // Get the first date epoch object document.write((second.getTime())/1000); // get the actual epoch values diff= second - first ; one_day_epoch = 24*60*60 ; // calculating one epoch if ( diff/ one_day_epoch > 365 ) // check , is it exceei { alert( 'date is exceeding one year'); } 

如果你正在寻找一种以年,月,日为组合的差异,我会build议这个function:

 function interval(date1, date2) { if (date1 > date2) { // swap var result = interval(date2, date1); result.years = -result.years; result.months = -result.months; result.days = -result.days; result.hours = -result.hours; return result; } result = { years: date2.getYear() - date1.getYear(), months: date2.getMonth() - date1.getMonth(), days: date2.getDate() - date1.getDate(), hours: date2.getHours() - date1.getHours() }; if (result.hours < 0) { result.days--; result.hours += 24; } if (result.days < 0) { result.months--; // days = days left in date1's month, // plus days that have passed in date2's month var copy1 = new Date(date1.getTime()); copy1.setDate(32); result.days = 32-date1.getDate()-copy1.getDate()+date2.getDate(); } if (result.months < 0) { result.years--; result.months+=12; } return result; } // Be aware that the month argument is zero-based (January = 0) var date1 = new Date(2015, 4-1, 6); var date2 = new Date(2015, 5-1, 9); document.write(JSON.stringify(interval(date1, date2))); 
 Date.prototype.addDays = function(days) { var dat = new Date(this.valueOf()) dat.setDate(dat.getDate() + days); return dat; } function getDates(startDate, stopDate) { var dateArray = new Array(); var currentDate = startDate; while (currentDate <= stopDate) { dateArray.push(currentDate); currentDate = currentDate.addDays(1); } return dateArray; } var dateArray = getDates(new Date(), (new Date().addDays(7))); for (i = 0; i < dateArray.length; i ++ ) { // alert (dateArray[i]); date=('0'+dateArray[i].getDate()).slice(-2); month=('0' +(dateArray[i].getMonth()+1)).slice(-2); year=dateArray[i].getFullYear(); alert(date+"-"+month+"-"+year ); }