javascript 如何使用angularjs检查所选时间小于当前时间或大于当前时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34236429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check selected time is less than current or greater than current time using angularjs?
提问by martin kumar
have used timepicker i got some time here i want to check that selected time is less than current time or gratter than current time.for example i have selected 11:00 AM ; var selectedtime='11:00 AM' how can i check 11:00 AM is less than current time or gratter than current time .i need to show alert i new to this technology help me out
使用了 timepicker 我在这里有时间我想检查所选时间是否小于当前时间或大于当前时间。例如,我选择了上午 11:00;var selectedtime='11:00 AM' 我如何检查 11:00 AM 是否小于当前时间或大于当前时间。我需要显示警报,我对这项技术不熟悉,请帮助我
$scope.date="Sat Dec 12 2015 00:00:00 GMT+0530 (India Standard Time)";
$scope.time = "11:00 AM";
for this particular date selected time is past or future i need to check how can i get current time and check with selected time pls some one help me out
对于这个特定日期,选择的时间是过去或未来,我需要检查如何获取当前时间并检查所选时间,请有人帮助我
i have tried but i cant able to solve pls help me out
我试过了,但我无法解决请帮帮我
var now = new Date();
var nowTime = new Date('1/1/1900 ' + now.toLocaleTimeString(navigator.language, {
hour: '2-digit',
minute: '2-digit',
hour12: true
}));
console.log(nowTime);
回答by A???
You can easily compare two Dates in javascript by using their timestamp values.
您可以Date使用时间戳值轻松比较javascript 中的两个s。
Call .getTime()on the date and it'll return the number of milliseconds passed since Jan 01 1970.
调用.getTime()日期,它将返回自 1970 年 1 月 1 日以来经过的毫秒数。
var now = new Date();
var d = new Date( ... ); // pass all the parameters you need to create the time
if (now.getTime() > d.getTime()) {
// the date stored in `d` is in the past.
}
If you just need to compare the hours and minutes, you can call .getHours()and .getMinutes()for example. Then calculate the total number of minutes d.getHours()*60+d.getMinutes()then compare this value.
如果你只需要比较小时和分钟,你可以打电话.getHours()和.getMinutes()例如。然后计算总分钟数,d.getHours()*60+d.getMinutes()然后比较这个值。
var now = new Date();
var d = new Date( ... );
var nowTime = now.getHours()*60+now.getMinutes();
var dateTime = d.getHours()*60+d.getMinutes();
// compare nowTime and dateTime
if (newTime > dateTime) {
// ....
}
After reading your comment.
看完你的评论后。
var getResult = function () {
var now = new Date();
var nowTime = new Date((now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear() + " " + now.getHours()+":"+now.getMinutes());
var userTime = new Date((now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear() + " " + selectedTime);
if (nowTime.getTime() > userTime.getTime()) {
return "passed";
}else if (nowTime.getTime() == userTime.getTime()) {
return "present";
}else {
return "future";
}
}
For angular version, check this http://plnkr.co/edit/UmpfBFR1zvL7qs9LpOjO?p=preview
对于角度版本,请查看http://plnkr.co/edit/UmpfBFR1zvL7qs9LpOjO?p=preview
回答by jwatts1980
Since you are comparing this against the current day, you can build a new Date()object using today's info plus the time that is entered.
由于您将其与当天进行比较,因此您可以Date()使用今天的信息加上输入的时间来构建新对象。
var time = "11:00 PM";
var dt = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear() + " " + time;
Then compare it against now:
然后将其与现在进行比较:
var now = new Date();
var userval = new Date(dt);
if (now > userval) //do logic

