最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check selected time is less than current or greater than current time using angularjs? - Stack Overflow

programmeradmin3浏览0评论

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

$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);

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

$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);
Share Improve this question edited Dec 12, 2015 at 17:58 martin kumar asked Dec 12, 2015 at 4:57 martin kumarmartin kumar 651 gold badge2 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You can easily pare two Dates in javascript by using their timestamp values.

Call .getTime() on the date and it'll return the number of milliseconds passed since Jan 01 1970.

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 pare 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 pare this value.

var now = new Date();
var d = new Date( ... );

var nowTime = now.getHours()*60+now.getMinutes();
var dateTime = d.getHours()*60+d.getMinutes();

// pare nowTime and dateTime
if (newTime > dateTime) {
    // ....
}

After reading your ment.

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

Since you are paring this against the current day, you can build a new Date() object using today's info plus the time that is entered.

var time = "11:00 PM";
var dt = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear() + " " + time;

Then pare it against now:

var now = new Date();
var userval = new Date(dt);
if (now > userval) //do logic

Here is an example Fiddle

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论