How To Date Sub Year Using Node Js?

Dec 24, 2021 . Admin



Hello Friends,

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

Here i will give you many example how to date sub year 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 subYear = new Date();
subYear.setFullYear(subYear.getFullYear() - 1);

var getYear = subYear.toLocaleString();

console.log(getYear);

now you can simply run by following command:

node server.js
Output :
16/12/2020, 4:52:52 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

var subYear = new Date('2020/12/22');
subYear.setFullYear(subYear.getFullYear() - 1);

var getYear = subYear;

console.log(getYear);

now you can simply run by following command:

node server.js
Output :
2019-12-21T18:30:00.000Z

It will help you...

#Node JS