function isMarketOpen(loc, tz, open, close, date = new Date()) {
// Get time in particular location for given date
let f = new Intl.DateTimeFormat('default', {
hour: '2-digit',
minute: '2-digit',
timeZone: tz,
timeZoneName: 'short'
});
let {hour, minute, timeZoneName} = f.formatToParts(date).reduce((acc, part) => {
if (part.type != "literal") {
acc[part.type] = part.value;
}
return acc;
}, Object.create(null));
// Return based on whether it's between open and close or not
let time = hour*60 + minute*1;
return `${loc} is ${time >= open && time <= close? 'open':'closed'} (${hour}:${minute} ${timeZoneName}).`;
}
// Sample data
let data = [
{loc: 'New York',
tz: 'America/New_York',
open: 540, // Minutes
close: 1020
},{
loc: 'London',
tz: 'Europe/London',
open: 480,
close: 960
}
];
// Show open/closed
data.forEach(obj => console.log(
isMarketOpen(obj.loc, obj.tz, obj.open, obj.close)
));
2条答案
按热度按时间7tofc5zh1#
问题在于你对人力资源的评估会导致状态。正如所写的,状态总是红色的。这里有一个解决办法:
第一个
z0qdvdin2#
你的问题有答案,但你的代码看起来比它需要的复杂得多。考虑利用Intl.DateTimeFormat构造函数和formatToParts。下面的代码只使用时间,你可能想包括星期几、假日等。