gpt4 book ai didi

javascript - 验证 JS 中开放时间之间的日期

转载 作者:行者123 更新时间:2023-12-02 19:04:23 25 4
gpt4 key购买 nike

我想知道验证日期是否在商店正确营业时间内的最佳方法是什么......

我正在为高尔夫专业人士创建一个网站,并接受该网站的类(class)预订。我使用 fullCalendar 作为日历,并将其与谷歌日历集成,这样它就可以轻松同步到专业人士的手机等。

作为日历的一部分,当客户单击时间段时,fullcalendar 会向我提供一个日期对象。我想验证这个选定的时间段是否在商店的营业时间范围内,以便参加类(class)等。

有时会从数据库表中检索时间,但如果效果最好,我可以将它们以 JSON 形式返回到页面上 - 但这不是这个问题的目的......

开放时间根据星期几和“季节”[夏季时间与冬季时间]而有所不同

ie:
Summer Times
Monday = 10:00 - 21:00
Tuesday = 10:00 - 21:00
Wednesday = 10:00 - 21:00
Thursday = 10:00 - 21:00
Friday = 10:00 - 21:00
Saturday = 08:00 - 18:00
Sunday = 08:00 - 18:00

Winter Times
Monday = CLOSED
Tuesday = 10:00 - 18:30
Wednesday = 10:00 - 18:30
Thursday = 12.00 – 20.30
Friday = 12.00 – 20.30
Saturday = 08:00 - 18:00
Sunday = 08:00 - 18:00

验证这一点的“最佳”方法是什么?

最好的选择是简单地执行嵌套的 if/switch 语句..

[sudo code]
if(summer}
{
switch(date.getDay())
{
case 1:
if(time > mondayOpeningTime && time < mondayCloseTime)
{
return true;
}
else
{
return false;
}
break;
case 2:
execute code block 2
break;
.....
default:
code to be executed if n is different from case 1 and 2
}
}
else
{
// Same as summer logic but for winter times etc..
}

或者有更好的方法吗?

对冗长的帖子/问题表示歉意,但提前感谢所有花时间阅读和回复它的人:)

克里斯

最佳答案

这是我写的。相当粗略,但考虑到了午夜之后关门时间的情况。

也可以在 Gist 上找到:https://gist.github.com/vtntimo/b5c724371bfb27bfb080

如果您进行改进,请与我联系:)

/*
Operating hours check
=====================================

- Checks if something is open right now
- Supports closing times going over midnight
- Feel free to use in whatever you need :)
- If you make improvements, please inform me!

Example:

// Setting operating hours
// Key 0 is sunday, 1 is monday and so on
var hours = [
// Sunday
{
open: '08:00',
close: '16:00'
},

// Monday
{
allday: true // Open all day
},

// Tuesday
false, // Closed all day

// Wednesday
{
open: '08:00',
close: '16:00'
},

// Thursday
{
open: '08:00',
close: '16:00'
},

// Friday
{
open: '08:00',
close: '16:00'
},

// Saturday
{
open: '08:00',
close: '16:00'
},
];

if(isOpen(hours)) {
alert("We're open!");
}
else {
alert("We're closed!");
}
*/
function isOpen(hours) {
var now = new Date();
var today = now.getDay();

var yesterday = today - 1;
if(yesterday < 0) yesterday = 6;

// Check today's opening hours
if(hours[today]) {

// It's open all day today
if(hours[today].allday) {
return true;
}

// Not open all day
else {
var open = hours[today].open;
var close = hours[today].close;

// Check if the location is open
if(checkOpenHours(now,now,open,close)) {
return true;
}
}

}

// Check yesterday's opening hours in case of closing after midnight
if(hours[yesterday]) {

// Not open all day (possibly closing after midnight)
if(!hours[yesterday].allday) {
var open = hours[today].open;
var close = hours[today].close;

var yesterday_date = new Date(now.getFullYear(), now.getMonth(), (now.getDate()-1), 0, 0, 0, 0);
if(checkOpenHours(now,yesterday_date,open,close)) {
return true;
}
}

}

// Not open
return false;
}

/*
Check if "now" is within operating hours
*/
function checkOpenHours(now, operatingDate, open, close) {
// Splitting times to array
var o = open.split(":");
var c = close.split(":");

// Hours not in proper format
if(o.length < 2 || c.length < 2) {
return false;
}

// Converting array values to int
for(var i = 0; i < o.length; i++) {
o[i] = parseInt(o[i]);
}
for(var i = 0; i < c.length; i++) {
c[i] = parseInt(c[i]);
}

// Set opening Date()
var od = new Date(operatingDate.getFullYear(), operatingDate.getMonth(), operatingDate.getDate(), o[0], o[1], 0, 0);

// Set closing Date()
var closingDay = operatingDate.getDate();

// Closing after midnight, shift day to tomorrow
if(o[0] > c[0]) {
closingDay++;
}
var cd = new Date(operatingDate.getFullYear(), operatingDate.getMonth(), closingDay, c[0], c[1], 0, 0);

// Is within operating hours?
if(now > od && now < cd) {
return true;
}
else return false;
}

关于javascript - 验证 JS 中开放时间之间的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com