• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How To Calculate A Javascript Date X Months Ago With Vanilla Javascript

Javascript · August 17, 2020

Working with dates in 2020 are still a mess. Presumably, they’ll also be a mess to work within 2021, 2022 and for a good while after that. Many (myself included) reach for a date library to fill the gaps.

For years Moment.js reigned supreme and for good reason, it can do everything with dates, including working with different time zones. Unfortunately, Moment can also result in bundle bloat.

And then came along date-fns. It could do almost everything Moment did and was good enough for 95% of all date and time related use cases. However, depending on the task, you can still use native vanilla Javascript to work with dates.

One such task I did recently was needing to calculate a day from today, my scenario was six months, but the solution I am going to show you can be changed to be any value and work for days and so on.

const SIX_MONTHS_AGO = new Date();
SIX_MONTHS_AGO.setMonth(SIX_MONTHS_AGO.getMonth() - 6);

We create a new date object, then we set the month to that of our month value minus 6 (where 6 is the number of months we want to go back). You can also change this so you can go 6 months into the future by changing the minus to a plus.

Finally, if you want to do date comparison and take a date and determine if it is older than six months, we can do something like this.

export const compareDates = (a: string | Date, b: string | Date): boolean => {
    const dateA = new Date(a).getTime();
    const dateB = new Date(b).getTime();

    return dateB > dateA;
}

This will determine if the second value supplied to our function is greater than the first value supplied. The first value is the base and the second value is the comparative value.

If you were to call compareDates with SIX_MONTHS_AGO as the first argument and new Date() as the second, we know the result would be true as today’s date is definitely older than six months ago.

compareDates(SIX_MONTHS_AGO, new Date())

No additional libraries required. I agree that the syntax is a little dry in comparison to the nicer API’s of Moment and date-fns, but it’s still a lighter and viable option.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Which Neural DSP Archetype Plugins Should You Buy?
  • Smoke Detector Randomly Goes Off Early Hours of The Morning
  • How To Mock uuid In Jest
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • Web 3.0 may have died before it even started
  • Deno Raises $21M - but is anyone using it yet?
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • How To Install Eufy Security Cameras Without Drilling or Using Screws

Recent Comments

  • Jay on Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • john on Deno Raises $21M – but is anyone using it yet?
  • Oranges on How To Store Users In Firestore Using Firebase Authentication
  • Precious on Fixing Sequel Pro SQL Encoding Error For Imported SQL Files
  • James on A List of WordPress Gutenberg Core Blocks

Copyright © 2022 · Dwayne Charrington · Log in

wpDiscuz