Utilizing Angular Filter to compare the date in a JSON file with the current system date

<p id="appt_time" ng-if="x.dateTime | date: MM/dd/yyyy == {{todaysDate}}">DISPLAY IF TRUE{{todaysDate}} </p>

Plunkr:https://plnkr.co/edit/r2qtcU3vaYIq04c5TVKG?p=preview

x.dateTime | date: MM/dd/yyyy retrieves a date and time which results in being filtered as: 06/18/2016 as per my json file.

My goal is to compare this "x.dateTime" with today's date and display the paragraph if the condition holds true. Hence, in my angular file, I set $scope.todaysDate = "06/18/2016", but the paragraph does not appear.

HTML file:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.angularjs.org/1.5.5/angular.js"></script>
    <script src="script.js"></script>

  </head>

  <body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="x in information">

  <!--Compare Json Object to Javascript Object-->
  <p ng-if ="x.dateTime">{{x.dateTime | date: MM/dd/yyyy }}</p> 
  <!--Compare Json Object to Today's Date-->
<p id="appt_time" ng-if="x.dateTime.valueOf() === todaysDate.valueOf()">Open on: {{todaysDate}} </p>
</div>
  </body>

</html>

JS File:

// declare a module
var myAppModule = angular.module('myApp', []);
  var todaysDate = new Date();


// configure the module.
// in this example we will create a greeting filter
myAppModule.controller('myCtrl', ['$scope','$http', function($scope, $http) 
{

    $scope.todaysDate = todaysDate;
    $scope.test = "Volvo";

    $http.get("time.json")
    .then(function(data) {
        $scope.information = data;
    });
}]
);

JSON file:

{
  "dateTime":"2016-06-18T18:41:00.748-04:00"

}

Answer №1

Working with dates and times can be quite challenging. I recommend utilizing the Moment.js library for a more straightforward solution:

ng-if="moment.utc(x.DateTime).isSame(todaysDate, 'day')"

The use of "day" specifies the level of detail in comparing the two values. Alternatively, you can achieve similar results without relying on an external library:

ng-if="x.DateTime.substring(0, 10) === todaysDate"

Simply update $scope.todaysDate to "2016-06-18".

Answer №2

To determine if a date is today, create a validate function that will return true. Remember to include $filter as well.

$scope.validate = function(date) {
   return $filter('date')(date, 'your format') === $filter('date')(new Date(), 'your format');
}

In the HTML code:

<p id="appt_time" ng-if="validate(x.dateTime)">SHOW ME IF TRUE{{x.dateTime}} </p>

Answer №3

Check out this link:

https://plnkr.co/edit/ZLJB5i1nQu9RYUV5FAX3?p=preview

// time.json

[{
  "dateTime":"2016-06-18T18:41:00.748-04:00"

}]

//script.js

 $http.get("time.json")
    .then(function(response) {
        $scope.information = response.data;
        debugger
    });

Your issue has been resolved

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What methods are most efficient for retrieving JSON data in regards to performance?

I've been utilizing HttpWebRequest to fetch json information from a third-party site, but the performance has been lacking. Would switching to wcf provide significant improvements? Your expertise and insights on this matter would be greatly appreciat ...

It seems that there is an issue with accessing the root directory while utilizing the yo

I'm currently working on setting up the Yeoman 1.0 beta's angular scaffolding and have been following these steps in my workflow: npm install generator-angular generator-testacular # installing generators yo angular # creati ...

Managing Related Data with JSONPlaceholder and MVC 5

Attempting to present a paginated table of information about "Albums" within an MVC 5 project, collecting data from various records sourced through the JSONPlaceholder REST API: View Source Users: (10 entries) { "id": 1, "name" ...

Display information based on the radio button chosen

I've set up a radio button with options for "no" and "yes", but neither is selected by default. Here's what I'm trying to achieve: If someone selects "no", nothing should happen. However, if they select "yes", then a message saying "hello w ...

Retrieve JSON data by making a POST request to a website's API

Can you help me retrieve Json data from a website API using JavaScript? I'm trying to fetch quotes from the following website: for my quotes generator page. While I have some understanding of making GET requests, it seems that this API requires POST ...

Having trouble getting Vue.js data to show up on the screen. I'm attempting to show a list of todos, but all that

I'm currently working on my App.vue file where I have set up the data for a todo list. However, despite creating an array of todos and attempting to display them, nothing is showing up on the screen. I'm at a standstill and could really use some ...

Error occurred during download or resource does not meet image format requirements

Encountering an issue when attempting to utilize the icon specified in the Manifest: http://localhost:3000/logo192.png (Download error or invalid image resource) from the console? import React from 'react'; import Logo from '../Images/logo1 ...

Counting the number of PHP inputs in a field

Hello, I am using a PHP script from Steve Dawson's website. To display the output on my HTML page, I am utilizing this AJAX script: <script> $.ajax({ type:'GET', url:'http://www.solariserat.se/count.php', data: ...

Is it possible to universally adjust the default content type for all error responses in Bottle framework?

I am currently building a compact REST API using Bottle. My goal is to exclusively return JSON in the response. I have considered using the @error(status_code) decorator for each HTTP status code to ensure that it outputs JSON, but I find this approach cu ...

Enhancing event listener using AngularJS and Jasmine: spying on callback functions

Can someone assist me with spyOnning a method connected to an event using scope.$on in a factory service, using Jasmine? The actual method is being called instead of the spy. Here is a plinkr I created showcasing the issue: http://plnkr.co/edit/2RPwrw?p=pr ...

I'm curious if there is a method to extract and retrieve color text information from JSON using Kotlin

I'm currently working on parsing and extracting data from JSON. However, I would like the respective color to be displayed instead of just the color name. For example: { "id": 1, "name": rose, "color ...

Is there a way to assign a value to an element based on whether I receive a true or false?

How can I display a true or false value in HTML when binding it? Code: <li ng-if="display"><a ng-click="logout();" ><span class="icon mail"> </span>Logout</a></li> <li ng-if="!display"><a href="#/login">< ...

Every time a GET request is made to the same route in Express.js, it seems to be stuck in a

Currently, I am working on an express application that requires a landing page to be rendered for the '/' route. This landing page consists of a text box and a submit button. The desired functionality is such that when a user enters some text int ...

Send in the completed form with your chosen preferences

Is there a way for me to submit a form with the selected option? My backend code is set up to work when using inputs: <input id="id_value_0" name="value" type="radio" value="1" /> <input id="id_value_1" name="value" type="radio" value="2" /> ...

Enhancing an existing Angular1 project to Angular4 while incorporating CSS, Bootstrap, and HTML enhancements

Facing CSS Issues in AngularJs to Angular4 MigrationCurrently in the process of upgrading an AngularJS project to Angular 4, specifically focusing on updating views. However, encountering issues with certain views not displaying the custom CSS from the the ...

Error message: "Unable to access D3 data when trying to select nested

Working with JSON data in D3 is proving to be a challenge for me. The file seems to be properly read, as indicated by its appearance when I use console.log, and it appears to be correctly formatted based on the examples I have come across. However, when I ...

Rearrange the layout by dragging and dropping images to switch their places

I've been working on implementing a photo uploader that requires the order of photos to be maintained. In order to achieve this, I have attempted to incorporate a drag and drop feature to swap their positions. However, I am encountering an issue where ...

What causes the error "property does not exist on type" when using object destructuring?

Why am I encountering an error in TypeScript when using Object destructuring? The JavaScript code executes without any issues, but TypeScript is showing errors. fn error: This expression is not callable. Not all elements of type '(() => void) | ...

Using an onClick event along with jQuery to change the CSS class style of another div

After searching extensively without success, I decided to register and ask my first question here. Hopefully, someone can provide a solution: My goal is to create a set of five buttons (divs) with onClick events that will show five different divs. I' ...

What is the best way to define the scope of an HTTP request within my application?

I need assistance with setting the scope for an http request in my Ionic App. Our Backend is built with Node.JS using the Hapi Framework. Since I primarily work on the frontend, I lack knowledge of server-side operations. Currently, I am able to successfu ...