Js计算两个日期之间的每一天[重复]
Posted
技术标签:
【中文标题】Js计算两个日期之间的每一天[重复]【英文标题】:Js Calculate every day between two dates [duplicate] 【发布时间】:2019-09-29 22:02:44 【问题描述】:我们认为我有以下日期:
from: 21/09/2019
to: 29/09/2019
我想要的结果是这样的:
[
"day": 21,
"month": 9,
"year": 2019
,
...
"day": 29,
"month": 9,
"year": 2019
]
如果我有这样的事情:
from: 21/09/2019
to: 7/10/2019
我想要的结果是这样的:
[
"day": 21,
"month": 9,
"year": 2019
,
...
"day": 30,
"month": 9,
"year": 2019
,
...
"day": 1,
"month": 10,
"year": 2019
"day": 7,
"month": 10,
"year": 2019
]
我想做的是在一个数组中打印开始日期和结束日期之间的所有天数。
我正在尝试这样的事情,但我遇到了一些问题:
let from = "23/09/2019";
let to = "7/10/2019";
function getDaysInMonth(month, year)
return new Date(year, month, 0).getDate();
function calculate(from, to)
let splitF = from.split("/");
let splitT = to.split("/");
let array = [];
let dF = parseInt(splitF[0]);
let dT = parseInt(splitT[0]);
let mF = parseInt(splitF[1]);
let mT = parseInt(splitT[1]);
let yF = parseInt(splitF[2]);
let yT = parseInt(splitT[2]);
let day, init, final;
while (true)
if (mF === mT && yF === yT)
day = dT - dF;
else
day = getDaysInMonth(mF, yF)-dF;
init = dF;
final = init + day;
Array.apply(null, length: final + 1 )
.map(Number.call, Number)
.slice(init)
.reduce((prev, curr) =>
let obj =
day: curr,
month: mF,
year: yF
;
array.push(obj);
return prev;
, []);
if (mF === mT && yF === yT) break;
mF++;
dF = 1;
if (mF === 13)
mF = 1;
yF++;
return array;
console.log(calculate(from, to));
【问题讨论】:
你看到this了吗?当你说问题时,你能描述一下你遇到的问题吗? 这是3题合一(解析、加1天、格式),之前都回答过很多很多次了。 【参考方案1】:您可以将日期加 1 到开始日期,Date 对象将负责验证新日期,即下个月或明年以及所有日期,重复直到结束日期 toDate
大于开始日期 fromDate
。
let from = "23/09/2019";
let to = "29/10/2019";
// convert to date object by interchanging date/month expected by Date function
let fromDate = new Date(from.replace(/([0-9]+)\/([0-9]+)/,'$2/$1'));
let toDate = new Date(to.replace(/([0-9]+)\/([0-9]+)/,'$2/$1'));
let response = [];
while(toDate > fromDate)
response.push(day: fromDate.getDate(), month: fromDate.getMonth()+1, year: fromDate.getFullYear());
fromDate.setDate(fromDate.getDate()+1) // increment date by 1
console.log(response)
【讨论】:
您的脚本似乎有问题,如果您从控制台看到返回值,则该值是从日期开始的:08/24/2019,但您创建的日期是从 09 /23/2019. 月是一个 0 索引数组。我更新了答案 如果我想包括第一天而不是排除它? 你绝对可以,推送到数组后递增。检查更新的答案 它对你有用吗?【参考方案2】:一种方法是通过拆分/
字符来提取输入日期字符串from
和to
的数字“部分”:
// Obtain day, month, year integers of the "from" date string
const [fromDay, fromMonth, fromYear] = from.split("/").map(part => Number.parseInt(part));
一旦将这些日期部分作为整数,日期范围就可以通过 for 循环进行迭代,在每次迭代中,您将根据该循环的当前迭代将项目对象添加到 result
数组:
// Add an item object with date number for current iteration
result.push( day, month, year );
每次迭代,您还需要检查下一个迭代日期是否等于 to
日期的“部分”数(以及根据需要增加当前月份和年份):
const numDaysInMonth = (month, year) => new Date(year, month, 0).getDate()
const extractParts = (date) => date.split("/").map(part => Number.parseInt(part));
const calcDaysBetweenDates = (from, to) =>
const [fromDay, fromMonth, fromYear] = extractParts(from);
const [toDay, toMonth, toYear] = extractParts(to);
const result = [];
// Iterate over from-to range starting at from year/month/day
for (let year = fromYear, month = fromMonth, day = fromDay;;)
// Add item object for current day/month/year to result array
result.push(
day,
month,
year
);
// Increment day
day++;
// If next day past current month, reset day, increment month
if (day > numDaysInMonth(month, year))
month++;
day = 1;
// If next month past current year, reset month, increment year
if (month > 12)
month = 1;
year++;
// If next day and month and year past to day/month/year, exit loop
if (day >= toDay && month >= toMonth && year >= toYear)
break;
// Add to date for range inclusivity
result.push(
day: toDay,
month: toMonth,
year: toYear
);
return result;
console.log(calcDaysBetweenDates("21/09/2019", "29/09/2019"));
console.log(calcDaysBetweenDates("21/09/2019", "7/10/2019"));
console.log(calcDaysBetweenDates("25/12/2019", "5/1/2020"));
【讨论】:
您的脚本似乎有问题,第二个日期:“09/21/2019”、“7/10/2019”它也给了我 09/31/2019,但这日期不存在。 @Paul 对此感到抱歉,正在计算下个月的天数而不是当前天数。刚刚更新的脚本 - 这有帮助吗?【参考方案3】:您可以将moment 与moment range 一起使用。
window['moment-range'].extendMoment(moment);
const start = moment('21/09/2019', 'DD/MM/YYYY');
const end = moment('29/09/2019', 'DD/MM/YYYY');
const range = moment.range(start, end);
for (let pt of range.by('day'))
console.log(pt.format('DD/MM/YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.2/moment-range.js"></script>
【讨论】:
以上是关于Js计算两个日期之间的每一天[重复]的主要内容,如果未能解决你的问题,请参考以下文章
oracle 如何查询两个时间段里的每一天数据之和。(只有开始日期和结束日期,没有每一天日期)