DEV Community

Cover image for Calculating A Rolling 18 Months
bob.ts
bob.ts

Posted on

3

Calculating A Rolling 18 Months

On a recent project, I was tasked initially with modifying an events.js file that loaded data from JSON.

Rolling 18 Months

Basically, they wanted me to filter the events list so that only events within the last 18 months showed.

The Data

The data looked something like this ...

[
  {
    "name": "A Conference",
    "type": "conference",
    "displayDate": "April 1, 2020",
    "sortDate": "2020/04/01"
  },
  {
    "name": "Another Conference",
    "type": "conference",
    "displayDate": "March 24, 2020",
    "sortDate": "2020/03/24"
  }
]
Enter fullscreen mode Exit fullscreen mode

The Solution

The original code looked like this ...

module.getEvents = function () {
  return $.getJSON('events.json', function (data) {
    module.events = data.sort(sortByProperty('sortDate')).reverse();
  });
};
Enter fullscreen mode Exit fullscreen mode

At some point in time, I added a function (deltaData) to my inventory. If anyone out there knows where this came from I would be happy to attribute it here. To fit this functionality, it became ...

module.deltaDate = function (start_date, days, months, years) {
  var date = new Date(start_date);
  date.setDate(date.getDate() + days);
  date.setMonth(date.getMonth() + months);
  date.setFullYear(date.getFullYear() + years);
  return date;
};
Enter fullscreen mode Exit fullscreen mode

From here, I needed to calculate back 18 months ...

module.setFilterDate = function () {
  var today = new Date();
  var dayChange = 0;
  var monthChange = -18;
  var yearChange = 0;
  return module.deltaDate(today, dayChange, monthChange, yearChange);
};
Enter fullscreen mode Exit fullscreen mode

Then, the original getEvents function transformed as follows. This function now filters, then sorts the incoming JSON data ...

module.getEvents = function () {
  var filterDate = module.setFilterDate();
  return $.getJSON('\\events.json', function (data) {
    var filtered = data.filter(function(item) {
      return new Date(item.sortDate) > filterDate;
    });
    module.events = filtered.sort(sortByProperty('sortDate')).reverse();
  });
};
Enter fullscreen mode Exit fullscreen mode

Conclusion

While this change was relatively simple, I wanted to document this functionality for my own future use. I hope you found this article interesting.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more