如何比较AngularJS中的两个日期? [英] How to compare two dates in AngularJS?

查看:198
本文介绍了如何比较AngularJS中的两个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我只想说我搜索Stack Overflow,找不到我的特定问题的答案,但是如果我错过了一个,我很抱歉,这是一个重复的。



所以我正在使用AngularJS做一个任务管理器,我有一个包含任务列表的表。这是我的表格代码:

 < thead> 
< tr>
< b> ID< / b>< / th>
< b>标题< / b>< / th>
< b>< b>开始日期< / b>< / th>
< b>< b>结束日期< / b>< / th>
< b> Type< / b>< / th>
< th>< / th>
< / tr>
< / thead>
< tbody>
< trng-repeat =任务跟踪$ index>
< td> {{task.id}}< / td>
< td> {{task.title}}< / td>
< td> {{task.start_date}}< / td>
< td> {{task.end_date}}< / td>
< td> {{task.type}}< / td>
< td>< a ng-click =remove(task)< i class =material-icons> clear< / i>< / a>< / td>
< / tr>
< / tbody>

如您所见,每个任务都有一个ID,标题,开始日期,结束日期,类型和那么该表在每行中都有一个按钮来删除任务。



这里是函数的代码 remove(task)

  $ scope.remove = function(task)
{
$ scope.tasks。 splice($ scope.tasks.indexOf(task),1);
};

现在这很重要。如果当前日期晚于结束日期,则应该删除任务,没有任何问题。但是,如果当前日期在前一个结束日期,则该程序需要启动一个带有字段的弹出窗口,以便用户可以提前取消取消任务的理由。



所以,我的问题是:如何比较结束日期​​与当前日期?



不用担心关于弹出窗口,我只需要帮助if语句。我会自己写出弹出窗口。



我也有 moment.js ,所以你可以使用它,如果你喜欢。



编辑:这是我的日期格式如何。我有一个datepicker以日期格式显示:YYYY-MM-DD。然后我有一个时间戳,给出这种格式的时间:hh:mm。这是我如何组合这两个:

  $ scope.tasks.push({
'id':tasks。 id,
'title':tasks.title,
'start_day':tasks.start_day,
'start_time':tasks.start_time,
'start_date':tasks.start_day + + tasks.start_time,
'end_day':tasks.end_day,
'end_time':tasks.end_time,
'end_date':tasks.end_day ++ tasks.end_time,
'type':tasks.type
});

所以datepicker日期存储在 tasks.end_day 并且timepicker时间存储在 tasks.end_time 中,并将它们与'end_date'组合:tasks.end_day ++ tasks.end_time ,所以最终日期格式为 YYYY-MM-DD hh:mm

解决方案

moment.js文档

  var isSameOrBefore = moment()。isSameOrBefore(task.end_date); 
var isBefore = moment()。isBefore(task.end_date);

在没有参数的情况下调用 moment()它使用当前日期。


First of all, I just want to say that I searched Stack Overflow and couldn't quite find the answer to my particular problem, but I apologize if I missed one and this is a duplicate.

So I'm making a Task Manager using AngularJS and I have a table with a list of tasks. This is my table code:

<thead>
    <tr>
        <th><b>ID</b></th>
        <th><b>Title</b></th>
        <th><b>Start Date</b></th>
        <th><b>End Date</b></th>
        <th><b>Type</b></th>
        <th></th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="task in tasks track by $index">
        <td>{{task.id}}</td>
        <td>{{task.title}}</td>
        <td>{{task.start_date}}</td>
        <td>{{task.end_date}}</td>
        <td>{{task.type}}</td>
        <td><a ng-click="remove(task)"<i class="material-icons">clear</i></a></td>
    </tr>
</tbody>

As you can see, each task has an ID, title, start date, end date, type and then the table has a button in each row to remove the task.

Here's the code for the function remove(task):

$scope.remove = function(task)
{
    $scope.tasks.splice($scope.tasks.indexOf(task), 1);
};

Now this is important. If the current date is later than the end date, the task should be removed with no problem. But, if the current date is before and end date, the program needs to launch a pop-up with a field so the user can write a justification for canceling the task early.

So, my question is: how do I compare the end date with the current date?

Don't worry about the pop up, I just need help with the "if statement". I'll write the pop up myself.

I also have moment.js so you can use that if you like.

EDIT: Here's how my dates are formatted. I have a datepicker which gives dates in this format: YYYY-MM-DD. And then I have a timepicker which gives times in this format: hh:mm. This is how I combine the two:

$scope.tasks.push({
            'id': tasks.id,
            'title': tasks.title,
            'start_day': tasks.start_day,
            'start_time':tasks.start_time,
            'start_date':tasks.start_day + " " + tasks.start_time,
            'end_day': tasks.end_day,
            'end_time': tasks.end_time,
            'end_date':tasks.end_day + " " + tasks.end_time,
            'type': tasks.type
        });

So the datepicker date is stored in the tasks.end_day and the timepicker time is stored in the tasks.end_time and I combine them both with 'end_date':tasks.end_day + " " + tasks.end_time so the final date is in the format YYYY-MM-DD hh:mm.

解决方案

From the moment.js documentation:

var isSameOrBefore = moment().isSameOrBefore(task.end_date);
var isBefore = moment().isBefore(task.end_date);

When calling moment() without a parameter it uses the current date.

这篇关于如何比较AngularJS中的两个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆