I have two input dates taking from Date Picker control. I have selected start date 2/2/2012 and end date 2/7/2012. I have written following code for that.
我有两个输入日期来自日期选择器控件。我选择了开始日期2/2/2012和结束日期2/7/2012。我已经为此编写了以下代码。
I should get result as 6 but I am getting 5.
我应该得到结果6但我得到5。
function SetDays(invoker) {
var start = $find('<%=StartWebDatePicker.ClientID%>').get_value();
var end = $find('<%=EndWebDatePicker.ClientID%>').get_value();
var oneDay=1000 * 60 * 60 * 24;
var difference_ms = Math.abs(end.getTime() - start.getTime())
var diffValue = Math.round(difference_ms / oneDay);
}
Can anyone tell me how I can get exact difference?
谁能告诉我如何才能得到确切的区别?
5 个解决方案
#1
342
http://momentjs.com/ or https://date-fns.org/
http://momentjs.com/或https://date-fns.org/
From Moment docs:
来自Moment docs:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // =1
or to include the start:
或者包括开头:
a.diff(b, 'days')+1 // =2
Beats messing with timestamps and time zones manually.
节拍手动弄乱时间戳和时区。
Depending on your specific use case, you can either
根据您的具体用例,您也可以
- Use
a/b.startOf('day')
and/ora/b.endOf('day')
to force the diff to be inclusive or exclusive at the "ends" (as suggested by @kotpal in the comments). - 使用/ b.startOf('day')和/或a / b.endOf('day')强制diff在“ends”处包含或排除(如注释中的@kotpal所示)。
- Set third argument
true
to get a floating point diff which you can thenMath.floor
,Math.ceil
orMath.round
as needed. - 将第三个参数设置为true以获得浮点diff,然后根据需要可以使用Math.floor,Math.ceil或Math.round。
- Option 2 can also be accomplished by getting
'seconds'
instead of'days'
and then dividing by24*60*60
. - 选项2也可以通过获得'秒'代替'天'然后除以24 * 60 * 60来实现。
#2
11
Try:
尝试:
//Difference in days
var diff = Math.floor(( start - end ) / 86400000);
alert(diff);
#3
5
If you are using moment.js you can do it easily.
如果您使用moment.js,您可以轻松完成。
var start = moment("2018-03-10", "YYYY-MM-DD");
var end = moment("2018-03-15", "YYYY-MM-DD");
//Difference in number of days
moment.duration(start.diff(end)).asDays();
//Difference in number of weeks
moment.duration(start.diff(end)).asWeeks();
If you want to find difference between a given date and current date in number of days (ignoring time), make sure to remove time from moment object of current date as below
如果您想在天数(忽略时间)内找到给定日期和当前日期之间的差异,请确保从当前日期的当前对象中删除时间,如下所示
moment().startOf('day')
To find difference between a given date and current date in number of days
查找给定日期和当前日期之间的天数差异
var given = moment("2018-03-10", "YYYY-MM-DD");
var current = moment().startOf('day');
//Difference in number of days
moment.duration(given.diff(current)).asDays();
#4
1
Try this Using moment.js (Its quite easy to compute date operations in javascript)
试试这个使用moment.js(在javascript中很容易计算日期操作)
firstDate.diff(secondDate, 'days', false);// true|false for fraction value
firstDate.diff(secondDate,'days',false); // true | false表示分数值
Result will give you number of days in integer.
结果将给出整数天数。
#5
0
Also you can use this code: moment("yourDateHere", "YYYY-MM-DD").fromNow(). This will calculate the difference between today and your provided date.
你也可以使用这段代码:moment(“yourDateHere”,“YYYY-MM-DD”)。fromNow()。这将计算今天和您提供的日期之间的差异。
#1
342
http://momentjs.com/ or https://date-fns.org/
http://momentjs.com/或https://date-fns.org/
From Moment docs:
来自Moment docs:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // =1
or to include the start:
或者包括开头:
a.diff(b, 'days')+1 // =2
Beats messing with timestamps and time zones manually.
节拍手动弄乱时间戳和时区。
Depending on your specific use case, you can either
根据您的具体用例,您也可以
- Use
a/b.startOf('day')
and/ora/b.endOf('day')
to force the diff to be inclusive or exclusive at the "ends" (as suggested by @kotpal in the comments). - 使用/ b.startOf('day')和/或a / b.endOf('day')强制diff在“ends”处包含或排除(如注释中的@kotpal所示)。
- Set third argument
true
to get a floating point diff which you can thenMath.floor
,Math.ceil
orMath.round
as needed. - 将第三个参数设置为true以获得浮点diff,然后根据需要可以使用Math.floor,Math.ceil或Math.round。
- Option 2 can also be accomplished by getting
'seconds'
instead of'days'
and then dividing by24*60*60
. - 选项2也可以通过获得'秒'代替'天'然后除以24 * 60 * 60来实现。
#2
11
Try:
尝试:
//Difference in days
var diff = Math.floor(( start - end ) / 86400000);
alert(diff);
#3
5
If you are using moment.js you can do it easily.
如果您使用moment.js,您可以轻松完成。
var start = moment("2018-03-10", "YYYY-MM-DD");
var end = moment("2018-03-15", "YYYY-MM-DD");
//Difference in number of days
moment.duration(start.diff(end)).asDays();
//Difference in number of weeks
moment.duration(start.diff(end)).asWeeks();
If you want to find difference between a given date and current date in number of days (ignoring time), make sure to remove time from moment object of current date as below
如果您想在天数(忽略时间)内找到给定日期和当前日期之间的差异,请确保从当前日期的当前对象中删除时间,如下所示
moment().startOf('day')
To find difference between a given date and current date in number of days
查找给定日期和当前日期之间的天数差异
var given = moment("2018-03-10", "YYYY-MM-DD");
var current = moment().startOf('day');
//Difference in number of days
moment.duration(given.diff(current)).asDays();
#4
1
Try this Using moment.js (Its quite easy to compute date operations in javascript)
试试这个使用moment.js(在javascript中很容易计算日期操作)
firstDate.diff(secondDate, 'days', false);// true|false for fraction value
firstDate.diff(secondDate,'days',false); // true | false表示分数值
Result will give you number of days in integer.
结果将给出整数天数。
#5
0
Also you can use this code: moment("yourDateHere", "YYYY-MM-DD").fromNow(). This will calculate the difference between today and your provided date.
你也可以使用这段代码:moment(“yourDateHere”,“YYYY-MM-DD”)。fromNow()。这将计算今天和您提供的日期之间的差异。