NiBu NiBu -4 years ago 115
AngularJS Question

How can I compare dates from a JSON object with dates in "5/17/16" format using angularjs?

I'm making a task management widget that displays tasks based on if they are in the current day, week or month. I'd like to push dates into weeklyTaskArray, dailyTaskArray, monthlyTaskArray and render them using .length.

This is what I've tried and the "if" statement runs but pushes all data into dailyQuoteArray/weeklyQuoteArray etc:

method: 'GET',
url: 'http://localhost:3000/api/quotes.json',
responseType: "json"
}).then(function(response) {
let quotes =;
angular.copy(quotes, quoteArray)

quotes.forEach(function (element) {
let currentDate = element.expiration_date;
var moment = require('moment')
var todaysDate = moment("05/17/2016");
var date2 = moment(currentDate)
var answer = moment(todaysDate).diff(date2, "days")
console.log("this is the answer", answer);

if (answer >= 2) {
if (answer <= 7) {
console.log("weeklyquotear length", weeklyQuoteArray.length);


Answer Source

You should convert your expression to javascript date object. You can use momentjs for date operations like converting or comparing.

var date = moment("05/17/2017", "DD/MM/YYYY")._d;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download