ReactHustle

How to Subtract or Add Date in Javascript

Jasser Mark Arioste

Jasser Mark Arioste

How to Subtract or Add Date in Javascript

I've seen a lot of tutorials on the internet on how to subtract date using plain javascript.This example explains the exact steps on how to do that. While it's useful for understanding how to work with dates on a foundational level, its not really practical when developing applications. In my opinion, it's better to use a date utility library like dayjs.

Dayjs doesn't only allow us to add or subtract dates, but also compare and format dates, and all other kinds of stuff that you can think of. 

Pros of using dayjs #

  1. Very lightweight library (2kb)
  2. Faster than any other date manipulation library like date-fns or luxon.
  3. Intuitive api
  4. You can add/remove features using its plugin system

Installing dayjs #

using yarn or npm

yarn add dayjs
or
npm i dayjs
123

using a cdn, insert this script to your html:

<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
1

How to add or subtract seconds in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z


const date1 = dayjs(date).subtract(1, 'second');
console.log(date1); //2022-10-15T01:04:18.169Z
12345678

How to add or subtract minutes in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z


const date1 = dayjs(date).subtract(1, 'minute');
console.log(date1); //2022-10-15T01:03:19.169Z
12345678

How to add or subtract hours in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z

const date1 = dayjs(date).subtract(1, 'hour');
console.log(date1); //2022-10-15T00:04:19.169Z
1234567

How to add or subtract days in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z

const date1 = dayjs(date).subtract(1, 'day');
console.log(date1); //2022-10-14T01:04:19.169Z
1234567

How to add or subtract weeks in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z

const date1 = dayjs(date).subtract(1, 'week');
console.log(date1); //2022-10-08T01:04:19.169Z
1234567

How to add or subtract months in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z

const date1 = dayjs(date).subtract(2, 'month');
console.log(date1); //2022-08-15T01:04:19.169Z
1234567

How to add or subtract years in Javascript using dayjs #

import dayjs from 'dayjs';

const date = new Date('2022-10-15T01:04:19.169Z');
console.log(date); //2022-10-15T01:04:19.169Z

const date1 = dayjs(date).subtract(3, 'year');
console.log(date1);  //2019-10-15T01:04:19.169Z
1234567

Resources #

Credits:

Image by David Mark from Pixabay

Share this post!

Related Posts

Disclaimer

This content may contain links to products, software and services. Please assume all such links are affiliate links which may result in my earning commissions and fees.
As an Amazon Associate, I earn from qualifying purchases. This means that whenever you buy a product on Amazon from a link on our site, we receive a small percentage of its price at no extra cost to you. This helps us continue to provide valuable content and reviews to you. Thank you for your support!
Donate to ReactHustle