Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在MomentJS中确定午夜后的营业时间_Javascript_Momentjs - Fatal编程技术网

Javascript 在MomentJS中确定午夜后的营业时间

Javascript 在MomentJS中确定午夜后的营业时间,javascript,momentjs,Javascript,Momentjs,我正在我的应用程序中使用,以确定某个企业当前是否已开业 我以以下格式存储我的工作时间: { open_time: "Thu Jan 01 1970 08:00:00 GMT-0500 (Eastern Standard Time)" }, { close_time: "Thu Jan 01 1970 23:00:00 GMT-0500 (Eastern Standard Time)" } 在我的代码中: let openTime = moment(weekday.open_time).form

我正在我的应用程序中使用,以确定某个企业当前是否已开业

我以以下格式存储我的工作时间:

{ open_time: "Thu Jan 01 1970 08:00:00 GMT-0500 (Eastern Standard Time)" },
{ close_time: "Thu Jan 01 1970 23:00:00 GMT-0500 (Eastern Standard Time)" }
在我的代码中:

let openTime = moment(weekday.open_time).format('HH:MM'); // 08:00
let closeTime = moment(weekday.close_time).format('HH:MM'); // 23:00
let currentTime = moment(new Date()).format('HH:MM'); // 20:00

// Determine if business is currently open
let isOpenNow = currentTime >= openTime && currentTime < closeTime ? true : false

有没有一种简单的方法来解释这个问题(或者简单的janejavascript逻辑)?

如果您对日期不感兴趣,那么将打开和关闭时间存储为时间,而不是日期。然后将打开和关闭时间转换为方便的格式,并与其他时间(可能是当前时间)进行比较。例如

var times=[{cafe:'Sally's',
开放时间:'08:00',//当天上午8点至晚上11点
关闭时间:'23:00'},
{咖啡馆:'弗雷德',
开放时间:'20:00',//晚上8点-第二天早上6点
关闭时间:'06:00'}];
函数isOpen(日期、时间){
//帮助器将HH:mm转换为毫秒
函数toMS(s){
s=s.split(“:”);
返回s[0]*3.6e6+s[1]*6e4;
}
//复制日期,以免影响原始日期
var d=新日期(+日期);
//从午夜开始计算约会时间
var t=日期-d.设定小时数(0,0,0,0);
返回t>=toMS(次打开时间)&&

t好的,解决方案满足我的需要:

let times = [{
    cafe: 'Sally\'s',
    open_time:'08:00', 
    close_time: '23:00'
}, {
    cafe: 'Fred\'s',
    open_time:'20:00', 
    close_time: '06:00'
}];

//it is simulated hour after midnight, but you can try with any time
let now = new Date();
now.setHours(2,0,0,0); // 02:00 AM

times.forEach((time: any) => {
    let openTime = moment(time.open_time, "HH:mm");
    let closeTime = moment(time.close_time, "HH:mm");
    if(openTime.isBefore(closeTime)) {
        //normal case
        let isOpen = moment(now).isBetween(openTime, closeTime, null, "[]");
        console.log(`${time.cafe} is ${isOpen ? 'open' : 'closed'}`);
    } else {
        /**
         * First: Note how open and close times switched places
         * Second: the ! in the boolean. If the time is between close and open time, it means the opposite, which is out of range.
         */
        let isOpen = !moment(now).isBetween(closeTime, openTime, null, "[]");
        console.log(`${time.cafe} is ${isOpen ? 'open' : 'closed'}`);
    }
})

我希望它能帮助别人!

我想你需要有日期和时间来确定关闭时间是否在当前时间之后。@NathanFisher似乎是这样。我正在查看文档中的任何“时间+1天”方法的种类目前的方式看起来像是在比较两个字符串。momentjs中有日期时间比较,您可以使用,但需要删除.format('HH:MM')为了能够使用这些。这就是你要找的@NathanFisher。我添加了我的原始日期格式,这可能会有所帮助。谢谢你,如果现在是午夜之后(例如凌晨3点),Fred's cafe返回错误(关闭),但应该开放。
let times = [{
    cafe: 'Sally\'s',
    open_time:'08:00', 
    close_time: '23:00'
}, {
    cafe: 'Fred\'s',
    open_time:'20:00', 
    close_time: '06:00'
}];

//it is simulated hour after midnight, but you can try with any time
let now = new Date();
now.setHours(2,0,0,0); // 02:00 AM

times.forEach((time: any) => {
    let openTime = moment(time.open_time, "HH:mm");
    let closeTime = moment(time.close_time, "HH:mm");
    if(openTime.isBefore(closeTime)) {
        //normal case
        let isOpen = moment(now).isBetween(openTime, closeTime, null, "[]");
        console.log(`${time.cafe} is ${isOpen ? 'open' : 'closed'}`);
    } else {
        /**
         * First: Note how open and close times switched places
         * Second: the ! in the boolean. If the time is between close and open time, it means the opposite, which is out of range.
         */
        let isOpen = !moment(now).isBetween(closeTime, openTime, null, "[]");
        console.log(`${time.cafe} is ${isOpen ? 'open' : 'closed'}`);
    }
})