有没有一种有效的方法来获取日期范围内按月份分组的天数?
例如,给定日期范围2020-01-30到2020-02-03,输出将是{ 'January': 2, 'February': 3 }。
发布于 2020-09-23 03:23:26
我认为没有比计算更有效的方法了。
const firstDateToPass = { year: 2020, month: 1, day: 26 };
const secondDateToPass = { year: 2020, month: 1, day: 29 };
const getCountOfDaysGroupedByMonth = (startDate, endDate) => {
const firstMonthDateTime = DateTime.fromObject(startDate);
const secondMonthDateTime = DateTime.fromObject(endDate);
if (firstMonthDateTime.month === secondMonthDateTime.month) {
// In same month
// Return difference in days
return {
[firstMonthDateTime.monthLong]: secondMonthDateTime.day - firstMonthDateTime.day
}
}
}
console.log(getCountOfDaysGroupedByMonth(firstDateToPass, secondDateToPass)) // { January: 3 }您只需要涵盖跨越多个月的案例,但我将把这一点留给您来解决?
https://stackoverflow.com/questions/64015703
复制相似问题