JavaScript使用Moment.js检查所选日期是今天的日期,时间是2pm
如何将今天的时间和日期与用户选择的日期进行比较.如果用户选择今天的日期或明天的日期和时间是下午2点或下午2点以上,那么我应该显示警告说交货时间已结束.
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.
我已经尝试过这样的事情
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!')
}
})
}
我有orders.start_date
是ng-repeat内的输入字段,所以我正在使用forEach
循环.我只想检查所选日期是今天还是明天.然后,我必须检查时间,如果它大于下午2点,那我就不应该允许.否则我可以允许.
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.
我不确定可接受的订购时间何时开始(因为检查日期是今天还是明天意味着从00:00到14:00都是公平游戏),但这是根据您的代码进行操作的一种方法:
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:
$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!')
}
})
}
请注意,isBetween(t1, t2)
不在可接受的范围内包括t1
和t2
.
Note, that isBetween(t1, t2)
does not include t1
and t2
into the accepted range.