Is there a way to display current data in angularJS based on the current time?

My database contains timestamps like 2016-12-30 00:30:10 which I retrieve using the $http module in Angular. Below is a snippet of my Angular code:

    angular
        .module('sampleApp')
        .controller('ReportCtrl', ReportCtrl)
        .factory('Reports', Reports)
        .filter('dateToISO', dateToISO);

    function ReportCtrl($scope, Reports) {
        $scope.returns = [];
        $scope.loadReturns = function () {
            Reports.getReturns().then(function (response) {
                $scope.returns = response.data;
            }, function (error) {
                console.log(error);
            })
        }
    }

function dateToISO() {
        return function (input) {
            input = new Date(input).toISOString();
            return input;
        };
    }

In the HTML:

        <tr ng-repeat="r in returns | orderBy:'-phdate'">
            <td ng-bind="r.product_name"></td>
            <td ng-bind="r.quantity"></td>
            <td ng-bind="r.username"></td>
            <td ng-bind="r.phdate | dateToISO | date:'medium'"></td>
        </tr>

The current output looks like this:

https://i.sstatic.net/PxsYM.png

Now, I want to display only the data with today's date, for example, if today is 12/30/2016. Should I handle this logic on the server-side or can it be done within Angular? Any assistance would be greatly appreciated.

Answer №1

If you want to achieve this using angular, follow the steps below:

HTML

    <tr ng-repeat="r in returns | orderBy:'-phdate'">
        <td ng-bind="r.product_name"></td>
        <td ng-bind="r.quantity"></td>
        <td ng-bind="r.username"></td>
        <td ng-bind="compareDate(r.phdate)"></td>
    </tr>

Javascript

    var compareDate = function (phdate) {
         // Initialize time to zero
         var curDate = new Date();
         curDate.setHours(0);
         curDate.setMinutes(0);
         curDate.setSeconds(0);

         var pDate = new Date(phdate);
         pDate.setHours(0);
         pDate.setMinutes(0);
         pDate.setSeconds(0);

         if (curDate === pDate) {
             return $filter('date')(pDate, 'medium');
         }
    }

Answer №2

    $scope.checkDate = function(checkdate){
        var dateObject = new Date();
        var monthNow = dateObject.getUTCMonth() + 1;
        var dayNow = dateObject.getUTCDate();
        var yearNow = dateObject.getUTCFullYear();
        var currentDateCheck = dayNow + "" + monthNow + "" + yearNow;

        var dateObjectToCheck = new Date(checkdate);
        var monthChecked = dateObjectToCheck.getUTCMonth() + 1;
        var dayChecked = dateObjectToCheck.getUTCDate();
        var yearChecked = dateObjectToCheck.getUTCFullYear();
        var checkedDate = dayChecked + "" + monthChecked + "" + yearChecked;

        if(currentDateCheck == checkedDate){
            return true;
        } else {
            return false;
        }
    }



<tr ng-repeat="r in returns | orderBy:'-checkdate'" ng-if="checkDate(r.checkdate)">
    <td ng-bind="r.item_name"></td>
    <td ng-bind="r.amount"></td>
    <td ng-bind="r.user"></td>
    <td ng-bind="r.checkdate"></td>
</tr>

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

Guide to inserting .json data into a .js file

Currently, I have an HTML5 banner designed using Flash CC. However, the platform in which I am showcasing this ad does not support JSON files. I am looking for a way to transfer the information from the JSON file into either my .html or .js file so that ...

Obtain a collection of custom objects through an HTTP GET request to be utilized in an Angular 2 application

I am currently grappling with the task of efficiently retrieving a list of custom objects and displaying their contents using an HTML file. Here is a simplified version of the HTTP GET method that I am working with: [HttpGet("/atr/testing")] public List& ...

An issue has been identified with the functionality of an Ajax request within a partial view that is loaded through another Ajax request specifically in

Here is the current scenario within my ASP.NET MVC application: The parent page consists of 3 tabs, and the following javascript code has been implemented to handle the click events for each tab: Each function triggers a controller action (specified in t ...

Ways to enhance filtering capabilities in AngularJS?

Currently, I am using the following ng-model: <input ng-model="query" type="text" iplaceholder="Add New Filter"></input> This is how I am presenting my data: <ul> <li role="menuitem" ng-repeat="item in availableFilters | filter:qu ...

Attempting to modify the state within a nested object located in an array by using json data

My objective is to retrieve data from the Google Books API, which returns JSON data in a similar format to what my state displays. I aim to update the title with the title string provided by the JSON data. Currently, I am encountering a "failed to compile" ...

Run several asynchronous HTTP requests in the background within a Chrome extension

I am facing a situation where I need to make multiple ajax requests through a Chrome extension and display the successful results in the popup HTML of the extension. I have created an array of URLs and loop through them to perform the ajax requests. Every ...

When the menu is selected, I would like for this div to both appear on the screen and slide

I need this div to be visible and move up when a menu item is selected. Can anyone provide a solution for this? Thank you in advance. <html><head> <script type="text/javascript"> <!-- var divobject = null; function init(id){ div ...

accessing the offsetTop property of React elements

When working in React, I've noticed that the offsetTop value of an element differs between componentDidMount and componentDidUpdate. This is surprising to me because I believed componentDidMount occurs after render, meaning the DOM elements should be ...

Using Angular's ng-show directive, create a custom Kendo Listview template to toggle the display of an

Currently, I am working with a Kendo Listview to display charts and would like to implement a feature where users can click on a checkbox to show or hide individual charts. Despite my efforts to use ng-show, I have been unsuccessful in making it work. Bel ...

Mobile display exhibiting glitches in animation performance

I have implemented an animation in the provided code snippet. const logo = document.querySelector('.logo'); const buttons = document.querySelectorAll('.loadclass'); const html = document.querySelector('html') const cornerme ...

Having trouble retrieving the full HTML code with the execute_script function in Python while web scraping

Currently, I am in need of HTML code to perform web scraping using Python. The particular website I am working with is that of a real estate agency. Prior to executing the onclick event for a button that changes pages, it is crucial for me to first obtain ...

Adding a class to individual elements generated through a v-for loop: a step-by-step guide

I have been working on a basic inventory "checker" that generates 14 divs labeled as either "active" or "inactive". Using Vue, I am able to create these divs with a simple v-for loop: <div class="inventory"> <div class="item" v-for="index in 14" ...

Issue with vue-template-compiler in Vue.js 3 webpack configuration

I am currently working on integrating vuejs 3 into a project that already utilizes webpack. I have been looking into using vue-loader as part of this process. After consulting the official documentation, I came across the following information: Every new ...

Instantly display selected image

I have encountered some challenges with my previous question on Stack Overflow as I couldn't find a suitable solution. Therefore, I decided to explore an alternative method for uploading images. My current goal is to upload an image immediately after ...

Error message 'Module not found' occurring while utilizing dynamic import

After removing CRA and setting up webpack/babel manually, I've encountered issues with dynamic imports. https://i.sstatic.net/CRAWr.png The following code snippet works: import("./" + "CloudIcon" + ".svg") .then(file => { console.log( ...

Only switch a radio button when the Ajax call results in success

Within an HTML form, I am working with a group of Radio buttons that trigger an Ajax call when the onchange() event is fired. This Ajax call communicates with the server to process the value sent by the call. The response can either be a string of "succes ...

What is the best way to display a resolved promise object in string format on a button, as an example?

I've searched through many similar questions, but none of them seem to address my specific issue in a simple way. Currently, the behavior of my promise is shown in the image below: When looking at lines 62 and 63 in the console, it's clear that ...

How can I choose which button to click in selenium if there are multiple buttons on the page with the same name, value, and id?

This particular button actually opens a popup, even though it is located on the same page. ![click here for image description][1] I attempted to interact with the element using the following code: driver.findElement(By.tagName("td")).findElement(By.id( ...

Browserify is unable to locate the 'jquery' module

While attempting to package my app with browserify, I encountered the following error message: Cannot find module 'jquery' from '/home/test/node_modules/backbone' I have searched for solutions to this issue, but none of them seem to ...

What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt: <v-row> <v-col ...