uda*_*aya 262 html javascript date
我正在计算'从'到'到'日期之间的天数.例如,如果起始日期是13/04/2010,并且截止日期是15/04/2010,则结果应为
如何使用JavaScript获得结果?
Max*_*xVT 508
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2008, 1, 12);
const secondDate = new Date(2008, 1, 22);
const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 44
这是一个执行此操作的函数:
function days_between(date1, date2) {
// The number of milliseconds in one day
const ONE_DAY = 1000 * 60 * 60 * 24;
// Calculate the difference in milliseconds
const differenceMs = Math.abs(date1 - date2);
// Convert back to days and return
return Math.round(differenceMs / ONE_DAY);
}
Run Code Online (Sandbox Code Playgroud)
小智 17
这是我使用的.如果您只是减去日期,它将无法在夏令时间界限内工作(例如4月1日至4月30日或10月1日至10月31日).这会减少所有时间,以确保您获得一天,并通过使用UTC消除任何DST问题.
var nDays = ( Date.UTC(EndDate.getFullYear(), EndDate.getMonth(), EndDate.getDate()) -
Date.UTC(StartDate.getFullYear(), StartDate.getMonth(), StartDate.getDate())) / 86400000;
Run Code Online (Sandbox Code Playgroud)
And*_*ich 11
这是我的实现:
function daysBetween(one, another) {
return Math.round(Math.abs((+one) - (+another))/8.64e7);
}
Run Code Online (Sandbox Code Playgroud)
+<date>确实的类型强制到整数表示,并且具有相同的效果<date>.getTime()和8.64e7是在一天的毫秒数.
Dam*_*ter 10
调整以允许夏令时差异.试试这个:
function daysBetween(date1, date2) {
// adjust diff for for daylight savings
var hoursToAdjust = Math.abs(date1.getTimezoneOffset() /60) - Math.abs(date2.getTimezoneOffset() /60);
// apply the tz offset
date2.addHours(hoursToAdjust);
// 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)
}
// you'll want this addHours function too
Date.prototype.addHours= function(h){
this.setHours(this.getHours()+h);
return this;
}
Run Code Online (Sandbox Code Playgroud)
我已经为另一个帖子写了这个解决方案,他们问,如何计算两个日期之间的差异,所以我分享了我准备的内容:
// Here are the two dates to compare
var date1 = '2011-12-24';
var date2 = '2012-01-01';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
date1_unixtime = parseInt(date1.getTime() / 1000);
date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// and finaly, in days :)
var timeDifferenceInDays = timeDifferenceInHours / 24;
alert(timeDifferenceInDays);
Run Code Online (Sandbox Code Playgroud)
您可以跳过代码中的一些步骤,我已编写它以便于理解.
你会在这里找到一个运行的例子:http://jsfiddle.net/matKX/
| 归档时间: |
|
| 查看次数: |
349751 次 |
| 最近记录: |