gpt4 book ai didi

javascript - 保持午夜过后的营业时间

转载 作者:行者123 更新时间:2023-12-01 04:09:43 25 4
gpt4 key购买 nike

例如:我在星期三21:00到08:30运行收银机,这意味着它在星期三 21:00打开并于星期四 08:30 关闭 我将收银机的工作时间存储为 0 - 1440 的分钟数:

"cashRegister" : {
"Monday" : {
"open" : 1260,
"close" : 510
},
"Tuesday" : {
"open" : 1140,
"close" : 420
},
"Wednesday" : {
"open" : 1260,
"close" : 510
},
"Thursday" : {
"open" : 1260,
"close" : 510
},
"Friday" : {
"open" : 1260,
"close" : 510
},
"Saturday" : {
"open" : 1260,
"close" : 510
},
"Sunday" : {
"open" : 1260,
"close" : 510
}
}

现在,例如,我在星期四 01:00 登录到我的系统,我的收银机在星期三 21:00 开始工作,我想返回以下内容期间:从:2017-01-11T21:00:00 到:2017-01-12T08:30:00问题是,如果我登录:2017-01-11T22:00:00 例如。我可以检查2017-01-11(星期三)收银机是否在午夜open > close工作,并添加 1 天以获得:2017-01- 12T08:30:00 但是,如果我在 2017-01-12T01:30:00 登录,我会在星期四如果我这样做,我最终会其中: 2017-01-12T21:00:00 - 2017-01-13T08:30:00 这是错误的,因为寄存器的工作时间为: 2017-01-11T21:00:00 到: 2017-01-12T08:30:00 而不是: 2017-01-12T21:00:00 到:2017-01-13T08:30:00 所以基本上我只需要了解收银机是否可以在午夜过后正常工作,而我们现在实际上已经过了午夜..我尝试这样做:

const cashRegister = {
"Monday" : {
"open" : 1260,
"close" : 510
},
"Tuesday" : {
"open" : 1140,
"close" : 420
},
"Wednesday" : {
"open" : 1260,
"close" : 510
},
"Thursday" : {
"open" : 1260,
"close" : 510
},
"Friday" : {
"open" : 1260,
"close" : 510
},
"Saturday" : {
"open" : 1260,
"close" : 510
},
"Sunday" : {
"open" : 1260,
"close" : 510
}
};

const todayUTC = moment.utc().startOf('day');
const day = todayUTC.format('dddd');
const registerToday = cashRegister[day];
const openingAt = moment.utc(registerToday.open * 60000);
const closingAt = moment.utc(registerToday.close * 60000);
const result = {};

result.openingAt = todayUTC.clone().set({
hour: openingAt.hours(),
minute: openingAt.minutes(),
second: 0,
})
result.closingAt = todayUTC.clone().set({
hour: closingAt.hours(),
minute: closingAt.minutes(),
second: 0,
}).add(openingAt > closingAt ? 1 : 0, 'days')

console.log(result) // perfect results

// if we check 1 minute after 00:00. we return bad results...

const minuteAfterTodayUTC = moment.utc().endOf('day').add(1, 'minute');
const day2 = minuteAfterTodayUTC.format('dddd');
const registerToday2 = cashRegister[day2];
const openingAt2 = moment.utc(registerToday2.open * 60000);
const closingAt2 = moment.utc(registerToday2.close * 60000);
const result2 = {};

result2.openingAt2 = minuteAfterTodayUTC.clone().set({
hour: openingAt2.hours(),
minute: openingAt2.minutes(),
second: 0,
})
result2.closingAt2 = minuteAfterTodayUTC.clone().set({
hour: closingAt2.hours(),
minute: closingAt2.minutes(),
second: 0,
}).add(openingAt > closingAt ? 1 : 0, 'days')

console.log(result2) // bad results
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

最佳答案

我猜“cashRegister”是收银机工作的时间,只要open大于close,那么它就会超过午夜。

如果您在任何特定时间访问寄存器,我猜您想要当前工作周期或最近完成的工作周期。将打开和关闭时间存储为分钟只会使人们更难计算出何时需要读取这些值,但我想可以简化编码。

您的代码似乎过于复杂,因此我将发布一个替代方案。对于任何日期,以下命令都会返回其所在的 session 或最近完成的 session 。因此,如果它正好在 session 开始之前,它将返回前一个 session 。

var sessions = {Monday    :{open: 1260, close: 510},
Tuesday :{open: 1140, close: 420},
Wednesday :{open: 1260, close: 510},
Thursday :{open: 1260, close: 510},
Friday :{open: 1260, close: 510},
Saturday :{open: 1260, close: 510},
Sunday :{open: 1260, close: 510}};

function getDayName(date) {
return 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' ')[date.getDay()];
}

function getSessionByDate(sessions, date) {

// Get the day name
var day = getDayName(date);

// Create dates for start and end, adjust later
var start = new Date(+date);
var end; // set later when start worked out

// Get the session for the current day
var session = sessions[day];

// If session start is after current time in minutes, then
// session started yesterday so set start and session to yesterday
var currentTimeInMinutes = date.getHours()*60 + date.getMinutes();

if (currentTimeInMinutes < session['open']) {
start.setDate(start.getDate() - 1);
day = getDayName(start);
session = sessions[day];
}
// Set start to session start
start.setHours(0,session['open'],0,0);
// For end, use start date and set hours to 24 and minutes to close
end = new Date(+start);
end.setHours(24, session['close'],0,0);

return [start,end];
}

[new Date(2017,0,10, 2,12,53), // Wednesday 10 January at 02:12:53 (during session)
new Date(2017,0,10, 9,12,53), // Wednesday 10 January at 09:12:53 (after session end)
new Date(2017,0,10,23,12,53), // Wednesday 10 January at 23:12:53 (during session)
new Date(2017,0,21,13,34,18) // Saturday 21 January at 13:34:18 (after session end)

].forEach(function(d) {
var session = getSessionByDate(sessions, d)
var opts = {weekday:'short', day:'2-digit',month:'short',year:'numeric',hour:'numeric',minute:'numeric'};
console.log('On ' + d.toLocaleString('en-GB', opts) + '\n' +
'Session start: ' + session[0].toLocaleString('en-GB', opts) +
'\nSession end : ' + session[1].toLocaleString('en-GB', opts)
);
});

关于javascript - 保持午夜过后的营业时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591412/

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