"Exploring the process of comparing dates using HTML, AngularJS, and Ionic

I am working on an HTML file that shows a list of notification messages. I am trying to figure out how to display the time difference between each notification. The code snippet below displays the notifications and includes the time for each one:

<ion-item ng-repeat="single in notifications_list track by $index" class="ng-scope" ng-class="single.read == true ? 'notification-read' : 'notification-unread'" ng-click="displayNotification(single)" style="padding:13px 16px 1px;">
        <div class="notifications">
            <p style="white-space:normal;font-size:16px;line-height:20px;">{{single.body}}</p>
            {{(currentDate - single.time).getDays()}}
            <time>{{ single.time | amCalendar:referenceTime:formats }}</time>
        </div>
    </ion-item>

In my code, I have already set

$rootScope.currentDate = new Date();

Essentially, I want to display the difference in days between two dates like this:

e.g. 2016-05-11T10:30:48.616Z - 2016-05-10T19:14:13.487+08:00
. Can someone guide me on how to achieve this in HTML?

If you have any advice on how I can implement this feature, please let me know. Thank you!

Answer №1

This could be useful for you. In the past, there was an angularjs plugin that would display time in a human-readable format... such as "a short while ago," "several hours ago," etc.

Check out this link

Answer №2

It appears that amCalendar is being utilized, indicating the use of the angular-moment plugin.

If this is the case, consider implementing the amDifference filter

An example implementation could be:

<time>{{ single.time | amDifference : null : 'days' }}</time>

Answer №3

Utilize the power of moment.js for Date and Time Calculations

<ion-item ng-repeat="item in notifications_list track by $index" class="ng-scope" ng-class="item.read == true ? 'notification-read' : 'notification-unread'" ng-click="showNotification(item)" style="padding:13px 16px 1px;">
    <div class="notifications">
        <p style="white-space:normal;font-size:16px;line-height:20px;">{{item.body}}</p>
        {{getDifference(item.time)}}
        <time>{{ item.time | amCalendar:referenceTime:formats }}</time>
    </div>
</ion-item>

In the controller:

$scope.getDifference=function(time){
var current=moment();
return  current.diff(time, 'days') 
};

Answer №4

If you need to format durations in your Angular application, consider using the angular-duration-format library.

To add angular-duration-format as a dependency in your app, include it in your module dependencies like this:

angular.module('myApp', [
'angular-duration-format'
]);

When working with templates, you can utilize the following syntax:

<p>
Time elapsed: {{ elapsed | duration:'hh:mm:ss:sss' }}<br/>
Formatted: {{ elapsedFormatted }}
</p>

In your controllers or other components, you can apply the duration filter:

angular.module('myApp').controller('exampleCtrl', function($scope, $filter) {
var durationFilter = $filter('duration');

$scope.elapsed = 123456789;
$scope.elapsedFormatted = durationFilter($scope.elapsed, 'hh:mm:ss:sss');

});

Regardless of where you use it, the result will be consistent:

Time elapsed: 34:17:36:789
Formatted: 34:17:36:789

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

Stop music with one click, code in Javascript

I am encountering an issue with a set of 5 div boxes on my website. Each box is supposed to play a specific audio track when clicked, based on data attributes. However, I'm having trouble pausing the previous track when clicking on a new box, resultin ...

HTML5 allows users to choose data from a list using a ComboBox and submit the 3-digit code

I'm looking to enhance my form by including an input box where users can select airports, similar to the functionality on this website (). When typing in the Destination Input, I want users to see a list of possible values with detailed suggestions su ...

Injecting Ajax-loaded content into an HTML modal

Hey there! I'm currently working on a project involving the IMDb API. The idea is that when you click on a film title, a popup should appear with some details about the movie. I've made some progress, but I'm stuck on how to transfer the mov ...

Drop-down functionality in Bootstrap Form becomes unresponsive after screen width is decreased

I recently encountered a strange issue with my simple Bootstrap form. When I resize my browser width to mobile size or view it on a mobile device, the form stops functioning properly. Unfortunately, I am unable to provide any specific code examples at this ...

Preparing data before using Kendo UI upload function is essential

I need to pass a data (specifically a GUID) to the upload method of the kendoUpload, so that a particular MVC Controller action method can receive this data each time the upload occurs. $("#propertyAttachmentUpload").kendoUpload({ async: { ...

"Return to the top" feature that seamlessly integrates with jQuery's pop-up functionality

Currently, I am facing an issue with a jQuery selectmenu list that opens as a popup due to its length. My goal is to add a "back to top" button at the end of the list. While researching online, I came across this tutorial which seems promising, but unfor ...

troubleshoot using Visual Studio Code

Here's the scenario: I need to debug a project using VS Code. The project is usually started by a batch file and then runs some JavaScript code. Now, I want to debug the JavaScript code, but I'm not sure how to do that. If anyone has any instruct ...

What could be the reason why I am unable to load the ejs file?

https://i.stack.imgur.com/91bPg.png Issue: Unable to find the "index.ejs" view in the views directory ...

Changes to a key value are not reflected in the array of objects

When making changes to input fields within an array of records that include Date and Text fields in a table, the data is not updating as expected. I am encountering issues where changing the Date input results in undefined Text values, and vice versa. My g ...

A guide on utilizing nodejs to automatically open the default browser and direct it to a specific web address

I am currently developing a program using Node.js. One of the features I aim to implement is the ability to launch the default web browser and direct it to a particular URL. I want this functionality to be compatible across Windows, Mac, and Linux operat ...

What is the best way to incorporate an npm module in a django-admin widget without the need to install node?

Background I am working on a Django app and need to create an admin widget. The widget will display text in a unique terminal-style format to show forwarded logs from an analytics process managed by Django (using the django-twined extension). To achieve ...

Using php variable to pass data to jquery and dynamically populate content in jquery dialog

I am facing challenges trying to dynamically display MySQL/PHP query data in a jQuery dialog. Essentially, I have an HTML table with MySQL results. Each table row has an icon next to its result inside an anchor tag with the corresponding MySQL ID. echo &a ...

Make sure to validate onsubmit and submit the form using ajax - it's crucial

Seeking assistance for validating a form and sending it with AJAX. Validation without the use of ''onsubmit="return validateForm(this);"'' is not functioning properly. However, when the form is correct, it still sends the form (page r ...

Indicator count for multiple slick carousels

Is there a way to create individual slide counters for multiple sliders that do not work together? In my current example, the slide counters are functioning together: https://codepen.io/anon/pen/oqqYjB?editors=1010 <div class="container"> <div ...

Which event listener in the DOM API should I use to trigger an action when a form field is focused, whether through a mouse click or by tabbing?

My aim is to increase the size of form fields when the cursor focuses on them, either through a mouse click or by tabbing using the keyboard. By utilizing document.activeElement focus, I can identify when the user clicks inside a text input or selects an ...

How to incorporate markdown files as strings in Next.js

Is there a way to bring in markdown files as strings in Next.js for use on both the client and server sides? ...

Creating a well-aligned form using Material-UI

Exploring Material-UI for the first time! How can a form be built where certain fields are arranged horizontally, others stacked vertically, and all aligned perfectly both vertically and horizontally? Check out this example image: https://i.sstatic.net/5R ...

Instruction not activating

Within my main.controller.js, I have a basic json object named $scope.sections. $scope.sections = [ { id: "landing", description: "<div><img pinch-zoom src='public/img/model.png'></div>" } ]; In the vie ...

What is causing my cookie script to malfunction?

I'm experiencing an issue with my cookie script on the following website: Upon visiting my site, you'll come across a hidden <div id="popupDiv"> Due to the display: none property in the CSS, it's not visible as intended. However, it ...

What is the best way to simulate global variables that are declared in a separate file?

dataConfiguration.js var userData = { URIs: { APIURI: "C" }, EncryptedToken: "D" }; configSetup.js config.set({ basePath: '', files:['dataConfiguration.js' ], ..... UserComponentDetails: .....some i ...