How To Date Sub Days Using Node Js?

Dec 20, 2021 . Admin



Hello Friends,

Now let's see example of how to date sub days example. We will check how to date sub days. This is a short guide on date sub days in node js. Let's get started with how to date sub days in node js.

Here i will give you many example how to date sub days using node js.

Example 1 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

var addDays = new Date();
console.log('Today is : ' + addDays.toLocaleString());

addDays.setDate(addDays.getDate() - 4);
console.log('4 days ago was : ' + addDays.toLocaleString());

now you can simply run by following command:

node server.js
Output :
Today is: 14/12/2021, 5:17:41 pm

4 days ago was: 10/12/2021, 5:17:41 pm

Example 2 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

var newDt = new Date().addDays(-4);

console.log(newDt);

now you can simply run by following command:

node server.js
Output :
2021-12-10T11:53:58.003Z

It will help you...

#Node JS