Generating a tally from JSON feedback using JavaScript

I'm looking to extract valuable data from a JSON obtained from web services that doesn't contain the required information. The JSON output resembles the following:

[ {
   "Val1": "Value One",
   "Status": "Okay",
   "endDate": "2017-09-30T00:00:00"
   }, 
   {
   "Val2": "Value Two",
   "Status": "Okay",
   "endDate": "2017-08-24T00:00:00"
} ]

Is there a method to calculate the difference in days between the current date (which is dynamic) and the endDate, then increment a counter by that calculated value?

This would be applied as follows:
- End date within 10 days: #
- End date within 20 days: #

and so forth.

Thank you in advance for any assistance provided.

Answer №1

Kindly refer to the code snippet provided below:

let endingDate = new Date(jsonArray[index].endDate.replace(/ /g, ''));
let timeDifference = endDate.getTime() - new Date().getTime();
let daysDifference = Math.floor(timeDifference / 1000 / 3600 / 24);

DEMO IN ACTION:

var data = [ {
   "Value1": "First Value",
   "Status": "Okay",
   "endDate": "2019-10-25"
   }, 
   {
   "Value2": "Second Value",
   "Status": "Okay",
   "endDate": "2019-09-30"
} ]

data.forEach(object => {
  let endingDate = new Date(obj.endDate.replace(/ /g, ''));
  let timeDifference = endingDate.getTime() - new Date().getTime();
  let daysDifference = Math.floor(timeDifference / 1000 / 3600 / 24);
  console.log(`End date is ${daysDifference} days away`);
});

Answer №2

One solution is to utilize the moment.js library

For example:

dateDifference = moment(date1).diff(date2, "days").format();

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

Incorporate JSON information into a sleek Bootstrap modal interface

I am looking to load a JSON file to generate a list within a Bootstrap Modal. The setup I have allows for the modal to appear when clicking on a person's image. <li class="project span3" data-type="pfa"> <a data-toggle="modal" data-targe ...

Unable to invoke setState (or forceUpdate) on a component that has been unmounted

After updating the component, I encountered an issue with fetching data from the server. It seems that componentWillUnmount is not helpful in my case since I don't need to destroy the component. Does anyone have a solution for this? And when should I ...

Looping through properties of objects with the help of angularJS ng-repeat is known as using objects['propertyname&#

What is the best way to iterate over an object with property names like this? $scope.myobjects = [ { 'property1': { id: 0, name: 'someone' } }, { 'property2': { id: 1, name: ' ...

Can we incorporate modulo into the loop?

I have a JavaScript function with HTML code inside. I need the ".one-card" div to repeat four times within each ".row" div. The ".row" div is being repeated in a foreach loop. I want to check if the result of modulo (4) is not equal to zero, then display t ...

Scraping data and converting it into JSON format using Scrapy

I am currently facing an issue where I need to extract text data from websites written in languages other than English, particularly Eastern European languages, using Scrapy. Once the scraping is done, it is crucial for me to convert the extracted data int ...

I'm encountering an issue where it appears that my ajax method may not be effectively communicating with my webservice

I am currently working on calling a webservice using an ajax request with the intention of retrieving clinical cases within a specific date range (for example, between 2015-01-01 and 2016-06-08). The webservice functions perfectly when tested individually. ...

Utilizing Angular 2+ to effectively manipulate the HTML Document Object Model with JavaScript in order to execute scripts

I'm facing an issue with inserting a script into my Angular project that has a specific format. <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-events.js"> { "width": "510", "height": "600", "impo ...

What is the best way to retrieve JSON data in ReactJS through ExpressJS?

Exploring the realms of reactjs and expressjs, I am seeking guidance on how to extract data from reactjs and store it in a variable. My current achievement includes successfully executing res.send to display the data. app.get('*', (req, res) =& ...

Using class binding for both ternary and non-ternary attributes

Suppose we have a tag that utilizes a ternary operator to apply alignment: <td :class="alignment ? ('u-' + alignment) : null" > This functions as intended since the pre-defined alignment classes are in place, now if we want to ...

Protractor struggles to locate mobile values in the HTML DOM despite Selenium's capabilities

I recently encountered an issue while attempting to scrape data for mobile test cases. The code snippet below works flawlessly for desktop, but on mobile it fails to find the desired element (resulting in an empty console log). Interestingly, when I print ...

The #each helper in Handlebars is used to iterate over an array

I have a function that generates an array as output. I am looking for a way to iterate over this array using the each method. Can anyone provide guidance on how to achieve this? Consider if the handlebars helper produces the following array: details: [{ ...

Customize JSON response formatting based on specific needs

When returning a JSON response to an API, I am utilizing the json_encode() method. The array includes a key for users that can contain multiple users, or be empty, and a key for course that can either be empty or contain a single object as shown below: { ...

Could there possibly be a glitch in the updated Spring JSON reader, or is it likely an error on my end?

I have set up the following reader: @Configuration public class ReaderConfig { @Bean public JsonItemReader<String> jsonItemReader(Resource resource) { return new JsonItemReaderBuilder<String>() .jsonObjectReader ...

nodemon keeps attempting to restart the server after making changes to files, but the updates are not being reflected

I've been using the nodemon package, but I'm experiencing issues with it not restarting the server properly. Instead of showing "server running" after making changes like in tutorials, all it displays is "restarting due to changes". This also res ...

When extracting text using .text(), remember to include spaces between td elements

Is there a method to include space between td's when using .text()? I have searched extensively on Google but could only find information on how to trim space. The code I am currently using is as follows: for (var i = 0, len = rows.length; i < l ...

Navigating React Router: Updating the page on back button press

Looking for a solution to a persistent issue. Despite various attempts and exhaustive research, the problem remains unresolved. Here's the situation: Within my React-Router-Dom setup, there is a parent component featuring a logo that remains fixed an ...

Displaying a loading spinner while waiting for the Vue.js app to render

Currently, I am developing a Vue.js application and I'm facing a challenge in adding a loading spinner before the app component is rendered. Instagram has a similar feature where they display their logo before showing the page contents. I have tried v ...

Is it a Mozilla Firefox glitch or something else?

After writing the code, I noticed a bug in Firefox and a small bug in Chrome. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

Challenge with uploading files on IPFS

Currently, I am utilizing the IPFS service for storing files. Through my Node.js application, I can create an IPFS_Node and proceed to upload files using this Node. var ipfs = ipfsClient({ // the hostname (or ip address) of the endpoint provid ...

Locate the file and add a data entry into an array within MongoDB with the help of Node.JS using M

Within my mongodb database, I have a collection named 'favorite' with the following schema: var favoritesSchema = new Schema({ postedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, dishes:[ ...