JavaScript
Double exclamation mark !! (not not) operator in JavaScript?
December 09, 2021
1 min
Working with dates in JavaScript can get a little tricky. I will show you how you can add 1 day to the current date to get tomorrow’s date. This even works when the month changes.
let today = new Date();
tomorrow
and pass the today
variable inside the Date
constructorlet tomorrow = new Date(today);
setDate
method on tomorrow
variable to add 1 day
to it. We do this by first getting the current date using tomorrow.getDate()
and adding 1 to it.tomorrow.setDate(tomorrow.getDate() + 1) // 1639464689038
This would return us a numeric value like 1639464689038
which is Unix epoch time
Similarily, if you want to set hours or reset it to 00:00:00
then you can call the setHours(0,0,0,0)
method as follows
tomorrow.setDate(tomorrow.getDate() + 1) tomorrow.setHours(0,0,0,0)
So without using any heavy library in your existing codebase you can easily add one date to the current date and get tomorrow’s date using JavaScript.
👉 If you found this post useful, bookmark this page (⌘ + d / ctrl + d) for your future reference 😃