Get Current Date And Time Example Using Node Js

Dec 13, 2021 . Admin



Hello Friends,

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

Here i will give you many example how to get current date and time 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 express = require('express');
var app = express();
  
var timeDt = new Date();
console.log(timeDt);
  
app.listen(3000);

now you can simply run by following command:

node server.js
Output :
2021-12-13T11:22:29.477Z

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 express = require('express');
var app = express();
   
var dtObj = new Date();
var day = ("0" + dtObj.getDate()).slice(-2);
var month = ("0" + (dtObj.getMonth() + 1)).slice(-2);
var year = dtObj.getFullYear();
   
var date = year + "-" + month + "-" + day;
console.log(date);
    
var hours = dtObj.getHours();
var minutes = dtObj.getMinutes();
var seconds = dtObj.getSeconds();
  
var timeDt = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
console.log(timeDt);
    
app.listen(3000);

now you can simply run by following command:

node server.js
Output :
2021-12-13
2021-12-13 17:9:23

It will help you...

#Node JS