How to Subtract or Add Date in Javascript
Jasser Mark Arioste
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 #
- Very lightweight library (2kb)
- Faster than any other date manipulation library like date-fns or luxon.
- Intuitive api
- 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 #
dayjs docs - https://day.js.org/docs/en/installation/installation
Credits:
Image by David Mark from Pixabay