使用 JavaScript 获取未来七天开闭馆日期数组

本文介绍如何使用 JavaScript 代码获取未来七天开闭馆日期数组,并提供示例代码。

代码示例:

const futureDates = ['2024-03-10', '2024-03-11', '2024-03-12', '2024-03-13', '2024-03-14', '2024-03-15', '2024-03-16'];
const closedDates = ['2024-03-12', '2024-03-15'];

const openingClosingDates = futureDates.map(date => {
  const isOpen = !closedDates.includes(date);
  return { date, isOpen };
});

console.log(openingClosingDates);

代码解析:

  1. futureDates 是未来7天的时间数组,格式为 'xxxx-xx-xx'。
  2. closedDates 是闭馆日期时间数组,格式为 'xxxx-xx-xx'。
  3. openingClosingDates 是未来7天的开闭馆日期数组对象,每个对象包含日期和是否开馆的信息。
  4. 代码使用 map 方法遍历 futureDates 数组,并判断当前日期是否在 closedDates 数组中,从而确定是否开馆。

使用说明:

请将代码中的 'xxxx-xx-xx' 替换为实际的日期。

注意:

本代码仅供参考,实际使用时可能需要根据具体情况进行调整。


原文地址: https://www.cveoy.top/t/topic/o5rC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录