不要有两个不同的时间属性,可以考虑使用
Unix time
。您可以修改日常数据对象,使其看起来像这样:
{id:1, name:"Yoga Lesson", time: 1690392600000, place:"Show Lounge"}
请注意,JS使用毫秒而不是秒,因此您必须将某些转换器给您的时间乘以1000。
在渲染时,可以使用
DateTimeFormat
,如以下示例:
date = data.time;
options = {
hour: "numeric",
minute: "numeric",
timeZone: "Australia/Sydney",
};
console.log(new Intl.DateTimeFormat("en-AU", options).format(date));
这使得比较非常容易。
if (date >= Date.now() && date <= Date.now() + 7200000) {
console.log("Within two hours from now!");
} else { console.log("Not within two hours from now."); }