问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/37707377/how-to-compare-two-dates-in-angularjs