我有一些日子。每一天都是開放/關閉時間的對象。 0 -1440分鐘。例如:做一定的計算工作時間返回兩個日期與時刻
const array = [
{
hours: {
open: 1260, // 21:00 <- same day
close: 510 // 08:30 <- next day
}
},
{
hours: {
open: 1260, // 21:00 <- same day
close: 510 // 08:30 <- next day
}
},
{
hours: {
open: 510, // 08:30 <- same day
close: 1260 // 21:00 <- same day
}
}
];
結果是這樣的:
// if current time is: "2017-01-10T14:53:45Z" for example
{
open: "2017-01-10T08:30:00Z",
close: "2017-01-11T21:00:00Z"
}
所以該機制應遍歷數組,找到最小開放,最大接近和了解哪一個是過了午夜添加/減去1天得到我提到的示例結果。我做了什麼至今:
const array = [
{
hours: {
open: 1260, // 21:00 <- same day
close: 510 // 08:30 <- next day
}
},
{
hours: {
open: 1260, // 21:00 <- same day
close: 510 // 08:30 <- next day
}
},
{
hours: {
open: 510, // 08:30 <- same day
close: 1260 // 21:00 <- same day
}
}
];
let range = {};
let temp_from = [];
let temp_to = [];
array.forEach((a) => {
const { hours } = a;
const open = moment.utc(hours.open * 60000).format('HH:mm'); // ex 21:00
const close = moment.utc(hours.close * 60000).format('HH:mm'); // ex 21:00
const todayUTC = moment.utc();
const hourUTC = todayUTC.format('H');
const minutesUTC = todayUTC.format('mm');
const secondsUTC = todayUTC.format('ss');
const CURRENT_TIME = (hourUTC * 60) + (minutesUTC * 1) + (secondsUTC/60); // exact time 0 - 1440
console.log('open', open.split(':')[0]);
temp_from.push(
moment.utc().set({
hour: open.split(':')[0],
minute: open.split(':')[1],
second: 0,
}).format()
)
temp_to.push(
moment.utc().set({
hour: close.split(':')[0],
minute: close.split(':')[1],
second: 0,
}).add(hours.open > hours.close ? 1 : 0, 'days').format()
)
})
console.log(temp_from, temp_to);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
我不知道的方法,並且該機制可以被打破?
這個問題是怎麼樣的混亂。你有三天的時間,但你想得到一個開放日期/時間和結束日期/時間的單一結果,但這些日期應該只相隔一天?爲什麼第一次和最後一次的日期相隔三天? –