Compare and filter data with date in MM/DD/YYYY format from current date using using new Date() while subtracting new Date() from user input value

Trying to compare the new Date(today's date) in 2015-02-21T21:48:48.137Z format with date in MM/DD/YYYY format which is my json data. I would like to filter all the data before today's days based on user input based on number of day's user enters(getting data before no certain number of days)..I can get the number of days and subtract it from todays date. Don't know how to compare this date with Json date value in MM/DD/YYYY format.Should i parse the Date() in order to compare with JSON data in MM/DD/YYYY format

I am able to take user input and subtract it from today's date based on 2015-02-20T21:48:48.137Z format using new Date().My data is MM/DD/YYYY format so need a way to compare the value i get after subtracting with date in MM/DD/YYYY format. Do i need to change parameter in my filter and input both date and value through ng-model.

// Code goes here

var app = angular.module('tempfilter', []);

app.controller('MainCtrl', function($scope) {
  $scope.sensordata = [{
    name: 'Rob',
    "Date": "2015-02-15",
    "Temp": 42
  }, {
    name: 'Bob',
    "Date": "2015-02-19",
    "Temp": 50
  }, {
    name: 'Tom',
    "Date": new Date().getDate(),
    "Temp": 60
  }, {
    name: 'Sinclair',
    "Date": new Date(),
    "Temp": 65
  $scope.filter = {
    value: 2


app.filter('tempo', function() {
  return function(items, field, value) {
    var filtered = [];
    var d = new Date(); // today!
    !d.setDate(d.getDate() - value); //date before today

    angular.forEach(items, function(item) {
      if (item[field] <= d) {
    return filtered;
<script src=""></script>
<!DOCTYPE html>
<html ng-app="tempfilter">

<head lang="en">
  <meta charset="utf-8">
  <script src=""></script>
    document.write('<base href="' + document.location + '" />');
  <script src="script.js"></script>


<body ng-controller="MainCtrl">
  Number of days before today
  <input type="number" ng-model="filter.value">
  <p id="demo">Showing values lower than {{ filter.value }}</p>
  Filtered list:
    <li ng-repeat="s in sensordata | tempo:'Date':filter.value">{{s.Date|date:'MM/dd/yyyy'}} {{}} {{s.Temp}}

  Full List:
    <li ng-repeat="s in sensordata ">{{s.Date|date}} {{}} {{s.Temp}}




You can parse a date in mm/dd/yyyy using:

function parseMDY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], b[0]-1, b[1]);

Note that IE 8 doesn't support parsing the format in the OP, it is generally considered a bad idea to use the Date constructor or Date.parse for parsing date strings.

Also, a string like 2015-02-15 may be parsed as local time in ES5 compliant browsers, and UTC in those compliant with the ed. 6 draft.

I don't understand you you've used:

!d.setDate(d.getDate() - value);

the ! operator seems redundant.


From the OP, it seems you're trying to parse dates like Feb 15, 2015, for that you can use:

// Parse MM d, y
function parseMDY(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
  var b = s.split(/[\s,]+/);
  return new Date(b[2], months[b[0].toLowerCase()], b[1]);

You can get the current date as new Date(), but that will also have the current time. You can zero the time part, subtract the required period, then compare.

E.g. if you want to filter for date strings more than say 2 days ago:

// Start with the current date and time
var epoch = new Date();

// Set the time to 00:00:00.0

// Subtract 2 days
epoch.setDate(epoch.getDate() - 2);

// Compare with string
if (epoch > parseMDY(dateString)) {
  // dateString is more than 2 days before epoch

Comparison between the current date and date in JSON data works. To compare the JSON date with the current date we could use javascript .getTime() method that returns the number of milliseconds between midnight of January 1, 1970 and the specified date.

We could use the same thing by using .getTime() for the JSON data and get the required time in milliseconds and compare the two dates. Although the logic works for MM/DD/YYYY adding time offset to the date might be good idea to get the precise date

Can use this link to get the details. I had posted a separate question similar to this query. Can we compare JSON data(MM/DD/YYYY format) in controller with today's date in custom filter and display filtered list in array


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.