js 已知未来七天时间数组 xxxx-xx-xx 和闭馆日期时间数组 xxxx-xx-xx 得到未来7天开闭馆日期的时间数组对象
你可以使用 JavaScript 的日期和时间对象来实现这个任务。首先,你需要将日期字符串转换为日期对象。然后,你可以使用日期对象的比较方法来确定开闭馆日期。
以下是一个实现的示例代码:
// 未来七天时间数组
const futureDates = ['xxxx-xx-xx', 'xxxx-xx-xx', 'xxxx-xx-xx', 'xxxx-xx-xx', 'xxxx-xx-xx', 'xxxx-xx-xx', 'xxxx-xx-xx'];
// 闭馆日期时间数组
const closedDates = ['xxxx-xx-xx', 'xxxx-xx-xx'];
// 将日期字符串转换为日期对象
const parseDate = (dateString) => {
const [year, month, day] = dateString.split('-');
return new Date(year, month - 1, day);
};
// 获取未来七天开闭馆日期的时间数组对象
const getOpeningHours = (futureDates, closedDates) => {
const openingHours = [];
for (let i = 0; i < futureDates.length; i++) {
const currentDate = parseDate(futureDates[i]);
const isOpen = !closedDates.some(date => parseDate(date).getTime() === currentDate.getTime());
openingHours.push({
date: futureDates[i],
isOpen: isOpen
});
}
return openingHours;
};
const openingHours = getOpeningHours(futureDates, closedDates);
console.log(openingHours);
这段代码会将未来七天的开闭馆日期存储在 openingHours 数组中,并输出到控制台。每个开闭馆日期对象具有 date 属性表示日期和 isOpen 属性表示是否开馆(true表示开馆,false表示闭馆)
原文地址: https://www.cveoy.top/t/topic/hCGK 著作权归作者所有。请勿转载和采集!