如何检查当前时间在工作时间之间

发布于 2025-02-07 04:32:02 字数 798 浏览 0 评论 0原文

我需要检查当前时间是否在代表的工作时间之间(不是外部库作为MomentJs)。

我有什么:

localTimeZone = 'America/New_York';
localWorkingHours = [
  {day: 'Sunday', from: '08:00', to: '14:00'},
  {day: 'Sunday', from: '23:00', to: '23:59'},
  {day: 'Monday', from: '00:00', to: '07:00'},
  {day: 'Tuesday', from: '08:00', to: '16:00'},
  {day: 'Wednesday', from: '08:00', to: '16:00'}
];

const now = new Date();
const currentDayInWeek = now.toLocaleString("en-US", {
    timeZone: localTimeZone,
    weekday: 'long'
});
const workingTimes = localWorkingHours.filter(times => times.day === currentDayInWeek);
workingTimes.forEach((time) => {
   // what is the best option to check that current time is between time.from and time.to ?
   // note that this code should run in another AWS region.
})

I need to check if the current time is between a representative's work hours (not with external library as momentJS).

What I have:

localTimeZone = 'America/New_York';
localWorkingHours = [
  {day: 'Sunday', from: '08:00', to: '14:00'},
  {day: 'Sunday', from: '23:00', to: '23:59'},
  {day: 'Monday', from: '00:00', to: '07:00'},
  {day: 'Tuesday', from: '08:00', to: '16:00'},
  {day: 'Wednesday', from: '08:00', to: '16:00'}
];

const now = new Date();
const currentDayInWeek = now.toLocaleString("en-US", {
    timeZone: localTimeZone,
    weekday: 'long'
});
const workingTimes = localWorkingHours.filter(times => times.day === currentDayInWeek);
workingTimes.forEach((time) => {
   // what is the best option to check that current time is between time.from and time.to ?
   // note that this code should run in another AWS region.
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-02-14 04:32:02

我这样做了:

const now = new Date();
const currentDayInWeek = now.toLocaleString("en-US", {
    timeZone: localTimeZone,
    weekday: 'long'
});
const currentTime = new Intl.DateTimeFormat([], {
    timeZone: localTimeZone,
    hour12: false,
    hour: '2-digit',
    minute: '2-digit'
}).format(now);

const workingTimes = localWorkingHours.filter(times => times.day === currentDayInWeek);

return workingTimes.some((time) => {
    if (currentTime >= time.from && currentTime <= time.to) return true;
});

I did this:

const now = new Date();
const currentDayInWeek = now.toLocaleString("en-US", {
    timeZone: localTimeZone,
    weekday: 'long'
});
const currentTime = new Intl.DateTimeFormat([], {
    timeZone: localTimeZone,
    hour12: false,
    hour: '2-digit',
    minute: '2-digit'
}).format(now);

const workingTimes = localWorkingHours.filter(times => times.day === currentDayInWeek);

return workingTimes.some((time) => {
    if (currentTime >= time.from && currentTime <= time.to) return true;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文