Sponsored

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. 

Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

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
Sponsored

Resources#

Credits:

Image by David Mark from Pixabay

Share this post!

Related Posts

Sponsored

Subscribe to our newsletter

Get up-to-date on latest articles on react.js, node.js, graphql, full-stack web development. No Spam. Helpful articles to improve your skills. Sent directly to your inbox.