Performing Javascript Date Arithmetic with the Unary Operator

Ole Ersoy
Jan 5, 2023

--

Image by Pexels from Pixabay

Scenario

We want to add one day to the current date.

Approach

First create a timestamp with the unary operator (+). Then add a day to it, and create a new date with the new timestamp as an argument.

const now: number = +new Date();
const ONE_DAY_MILLIS: number = 24 * 60 * 60 * 1000;
const tomorrow = new Date(now + ONE_DAY_MILLIS);
const date: Date = new Date(now);

As can be noted in the below demo doing +new Date() is the same thing as new Date().getTime() .

Demo

--

--