Add 3 days in milliseconds to the current date in JavaScript
In this tutorial, we are going to learn about how to add the 3 days in milliseconds to the current date in JavaScript with the help of examples.
Adding 3 days in milliseconds
To add the 3 days in milliseconds to the current date, first we need to multiple the 3 days in milliseconds eg: 1 day contains 86400000 milliseconds. so for 3 days (3x8640000) then add it to the current timestamp.
Here is an example:
const milliSecs = 86400000*3;
const currentTimeStamp = Date.now();
const date = new Date(currentTimeStamp + milliSecs);
console.log(date);
In the above example:
-
First, we have multipled the 86400000 milliseconds with 3. so we get the 3days in milliseconds.
-
Then we used the built-in
Date.now()
method to get the current timestamp.
At last we added the current timestamp and milliseconds and passed it to the new Date() constructor. so, we get the date object prior to the 3 days. eg if today is 4 September it returns the date by adding 3 days to it that is 7 September.
We can also write the above code in one line like this:
const date = new Date(Date.now()+86400000*3);
console.log(date);