使用Moment.js检查所选日期的JavaScript是今天的日期和时间是下午2点

时间:2022-08-25 10:52:17

How do I compare today's time and date with user selected date. If user selects today's date or tomorrow's date and time is 2pm or more than 2pm then I should show alert saying delivery time over.

如何将今天的时间和日期与用户选择的日期进行比较。如果用户选择今天的日期或明天的日期和时间是下午2点或超过下午2点,那么我应该显示警告说交货时间结束。

I have tried something like this

我尝试过这样的事情

$scope.checkDateTime=function(){                    
    angular.forEach($scope.orders.start_date,function(s){
        console.log(s);
        var curTime = moment().format('YYYY-MM-DD HH:mm:ss');
        var orderTime = s+' 14:00:00';
        console.log(moment(orderTime).diff(curTime,'seconds'));
        if(moment(orderTime).diff(curTime,'seconds')>86400) {
            console.log('hooray!')
        }
    })
}

I have orders.start_date is a input field inside ng-repeat so I am using forEach loop. I just want to check if selected date is today's or tomorrow's date. Then I have to check time, and if it's greater than 2pm then I should not allow. Otherwise I can allow.

我有orders.start_date是ng-repeat中的输入字段,所以我使用forEach循环。我只想检查所选日期是今天还是明天的日期。然后我必须检查时间,如果它超过下午2点,那么我不应该允许。否则我可以允许。

1 个解决方案

#1


1  

I am not sure when the acceptable order period starts (since checking if the day is today or tomorrow means that everything from 00:00 to 14:00 is a fair game) , but here is the is one way to do it based on your code:

我不确定可接受的订单期限何时开始(因为检查当天或明天是否意味着从00:00到14:00的所有内容都是公平的游戏),但这是基于您的一种方式码:

$scope.checkDateTime = function(){                    
    angular.forEach($scope.orders.start_date, function(s){
        console.log(s);
        var selectedTime = moment(s);

        // create boundaries for time ranges
        var today_end =  moment("14:00","HH:mm"); // today 14:00
        var today_start =  moment().subtract(1,'day').endOf('day'); // end of yesterday (since we need to include 00:00 of today)
        var tomorrow_end =  moment("14:00","HH:mm").add(1,'day'); // tomorrow 14:00
        var tomorrow_start =  moment().endOf('day'); // end of today (since we need to include 00:00 of tomorrow)

        // check if time in questions fits any of the ranges
        if( ( selectedTime.isBetween(today_start, today_end) ||
            ( selectedTime.isBetween(tomorrow_start, tomorrow_end) )
            console.log('hooray!')
        }
    })
}

Note, that isBetween(t1, t2) does not include t1 and t2 into the accepted range.

注意,即在(t1,t2)之间不包括t1和t2到可接受的范围内。

#1


1  

I am not sure when the acceptable order period starts (since checking if the day is today or tomorrow means that everything from 00:00 to 14:00 is a fair game) , but here is the is one way to do it based on your code:

我不确定可接受的订单期限何时开始(因为检查当天或明天是否意味着从00:00到14:00的所有内容都是公平的游戏),但这是基于您的一种方式码:

$scope.checkDateTime = function(){                    
    angular.forEach($scope.orders.start_date, function(s){
        console.log(s);
        var selectedTime = moment(s);

        // create boundaries for time ranges
        var today_end =  moment("14:00","HH:mm"); // today 14:00
        var today_start =  moment().subtract(1,'day').endOf('day'); // end of yesterday (since we need to include 00:00 of today)
        var tomorrow_end =  moment("14:00","HH:mm").add(1,'day'); // tomorrow 14:00
        var tomorrow_start =  moment().endOf('day'); // end of today (since we need to include 00:00 of tomorrow)

        // check if time in questions fits any of the ranges
        if( ( selectedTime.isBetween(today_start, today_end) ||
            ( selectedTime.isBetween(tomorrow_start, tomorrow_end) )
            console.log('hooray!')
        }
    })
}

Note, that isBetween(t1, t2) does not include t1 and t2 into the accepted range.

注意,即在(t1,t2)之间不包括t1和t2到可接受的范围内。