gpt4 book ai didi

javascript - 获取两个日期之间的月份

转载 作者:行者123 更新时间:2023-11-30 09:58:42 28 4
gpt4 key购买 nike

我不是在寻找标题中问题的答案或代码片段,而是在寻求帮助以理解为什么一段代码没有像我预期的那样工作:

var getMonthsBetween = function(date1, date2)
{
'use strict';

// Months will be calculated between start and end dates.
// Make sure start date is less than end date.
// But remember if the difference should be negative.
var start_date = date1;
var end_date = date2;
var inverse = false;

if (date1 > date2)
{
start_date = date2;
end_date = date1;
inverse = true;
}

// Calculate the differences between the start and end dates
var yearsDifference = end_date.getFullYear() - start_date.getFullYear();
var monthsDifference = end_date.getMonth() - start_date.getMonth();
var daysDifference = (end_date.getDate()+1) - start_date.getDate(); // This +1 might be a mistake

return (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + daysDifference/30); // Add fractional month
}

以上代码旨在返回两个给定日期之间的差异(以月为单位),精确到小数点后一位。不幸的是,我没有自己编写代码,也没有与编写它的人联系,而且我无法说出为什么作者决定在计算天数差异时决定在结束日期添加一天(有稍微偏离的情况)。

有人可以帮我解决这个问题吗?我不确定在这里添加额外一天的原因是什么 ((end_date.getDate()+1)) 并且我不确定它是否真的正确(我怀疑它不正确)但是我想听听其他意见。

编辑我不清楚该函数如何未按预期工作 - 一旦用户从 2 个日期选择器中选择 2 个日期,它就会被调用。计算出的数字不会发送到后端,而是会发回原来的 2 个日期,然后我们使用 (PHP) datetime.diff 计算出日期之间有多少个月。后端已经确认是正确的,但是前端并不总是匹配,例如

start date = Monday June 30, 2014, 00:00:00
end date = Saturday February 28, 2015, 00:00:00
result (JS) = 7.933333333333334
result (PHP datetime.diff) = 8.0333333333333

start date = Tuesday June 30, 2015, 00:00:00
end date = Monday February 29, 2016, 00:00:00
result (JS) = 7.966666666666667
result (PHP datetime.diff) = 8.0333333333333

然后有其他次数字确实匹配,例如

start date = Monday June 30, 2014, 00:00:00
end date = Wednesday April 15, 2015, 00:00:00
result (JS) = 9.5
result (PHP datetime.diff) = 9.5

最佳答案

显然 PHP 的 datetime.diff 在其计算中包括结束日期。当我实际增加 JavaScript 端的结束日期时,数字全部匹配。 比较它们之前增加日期对象本身会导致更改到下个月。最初的开发人员已经注意到发生了这种情况,但他没有正确地增加结束日期。他在结束日期的日期值中又添加了一天,但在向 2015 年 2 月 28 日添加一天时,下个月并没有发生变化

此代码按预期工作:

var getMonthsBetween = function(date1, date2)
{
'use strict';

// Months will be calculated between start and end dates.
// Make sure start date is less than end date.
// But remember if the difference should be negative.
var start_date = date1;
var end_date = date2;
var inverse = false;

if (date1 > date2)
{
start_date = date2;
end_date = date1;
inverse = true;
}

end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects.
end_date.setDate(end_date.getDate() + 1);

// Calculate the differences between the start and end dates
var yearsDifference = end_date.getFullYear() - start_date.getFullYear();
var monthsDifference = end_date.getMonth() - start_date.getMonth();
var daysDifference = end_date.getDate() - start_date.getDate();

return (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + daysDifference/30); // Add fractional month
}

关于javascript - 获取两个日期之间的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712292/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com