【问题标题】:How to check between two dates如何检查两个日期之间
【发布时间】:2021-11-01 02:45:22
【问题描述】:

我有两个不同的数组:

accomodation: [
  {
    id: 1,
    name: "Senator Hotel Fnideq",
    address: "Route de Ceuta, 93100 Fnidek, Morocco",
    checkin: "September 1",
    fullCheckinDate: "2021-09-01",
    checkout: "September 3",
    fullCheckoutDate: "2021-09-03",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  },
  {
    id: 2,
    name: "Kabek Morocco Hotel",
    address: "Omis Juy, 93100 Kabek, Morocco",
    checkin: "September 3",
    fullCheckinDate: "2021-09-03",
    checkout: "September 5",
    fullCheckoutDate: "2021-09-05",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  }
]
experiences: [
        {
            id: 1,
            fullDate: "2021-09-01",
            title: "Arrival",
            itinerary: // []
        },
        {
          id: 2,
          fullDate: "2021-09-02",
          title: "Sightseeing",
          itinerary: // []
        }
    ]

我想找到一种方法将住宿和体验之间相同的日期组合成一个对象。

myTrips: [
  {
    accomodation: {
      id: 1,
      name: "Senator Hotel Fnideq",
      address: "Route de Ceuta, 93100 Fnidek, Morocco",
      checkin: "September 1",
      fullCheckinDate: "2021-09-01",
      checkout: "September 3",
      fullCheckoutDate: "2021-09-03",
      nights: 2,
      mealplan: "Breakfast,Lunch"
    },
    experiences: {
       id: 1,
       fullDate: "2021-09-01",
       title: "Arrival",
       itinerary: // []
    }
  },
  //... another object
]

我正在使用dayjs 来帮助确定日期。我该怎么做呢

【问题讨论】:

  • 通常参考ISO 8601。绝对不是 01/09/2021,这是模棱两可的。
  • 要解决这个问题,您必须首先像@jarmod 建议的那样格式化两个数组中的日期,然后如果日期相同,则将数组合并到单独的对象中。您可以使用 reduce 或 lodash。或者您可以按日期对数组进行排序并合并它们
  • merging objects by ID 的一些想法。您可能会扩展到包括日期重叠。
  • 不知道你说的最后一句话是什么意思。可以给个代码sn-p吗?
  • 这里没有足够的信息。体验 ID 是否与住宿 ID 相关,还是相互独立?在同一家酒店的一次住宿可以有多种体验吗?您提出的结果(其中体验是单个对象)表明不能超过一个,但看起来抵达和观光都是在第一家酒店时的体验。

标签: javascript arrays vue.js object dayjs


【解决方案1】:

const accomodation = [
  {
    id: 1,
    name: "Senator Hotel Fnideq",
    address: "Route de Ceuta, 93100 Fnidek, Morocco",
    checkin: "September 1",
    fullCheckinDate: "2021-09-01",
    checkout: "September 3",
    fullCheckoutDate: "2021-09-03",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  },
  {
    id: 2,
    name: "Kabek Morocco Hotel",
    address: "Omis Juy, 93100 Kabek, Morocco",
    checkin: "September 3",
    fullCheckinDate: "2021-09-03",
    checkout: "September 5",
    fullCheckoutDate: "2021-09-05",
    nights: 2,
    mealplan: "Breakfast,Lunch"
  }
];

const experiences = [
    {
        id: 1,
        fullDate: "2021-09-01",
        title: "Arrival",
        itinerary: []
    },
    {
      id: 2,
      fullDate: "2021-09-02",
      title: "Sightseeing",
      itinerary: []
    }
];

const myTrips = [];
accomodation.map(acc => {
  const experience = experiences.filter(exp => {
  const date = new Date(exp.fullDate);
  return date >= new Date(acc.fullCheckinDate) && date <= new Date(acc.fullCheckoutDate);
});
  myTrips.push({accomodation: acc, experience});
});

console.log(myTrips);

【讨论】:

  • 我希望 2021-09-01 发生的经历应该与同一天的住宿一起发生。在我的问题中查看 myTrip 数组
  • 问题是住宿只有入住和退房日期,我需要知道中间日期的方法
  • 这个只给我一天,因为我们在同一家酒店住了2天,所以我需要第二天在同一个对象的住宿体验
猜你喜欢
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多