How to Get Current Date and Time in Node JS?

In this tutorial we will go over the demonstration of how to get date and time in node js. We will use node js get current date and time. let’s discuss about node js get current date yyyy-mm-dd with time. i would like to share with you node js get datetime now.

let’s see bellow examples to getting current date and time in node js.

Example 1:

server.js

var express = require('express');

var app = express();

var dateTime = new Date();

console.log(dateTime);

app.listen(3000);

Ouput

2021-06-17T03:56:37.789Z

Example 2:

server.js

var express = require('express');

var app = express();

var date_ob = new Date();

var day = ("0" + date_ob.getDate()).slice(-2);

var month = ("0" + (date_ob.getMonth() + 1)).slice(-2);

var year = date_ob.getFullYear();

var date = year + "-" + month + "-" + day;

console.log(date);

var hours = date_ob.getHours();

var minutes = date_ob.getMinutes();

var seconds = date_ob.getSeconds();

var dateTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;

console.log(dateTime);

app.listen(3000);

Ouput

2021-06-17

2021-06-17 9:26:37

Example 3:

install npm package

npm install node-datetime --save

server.js

var express = require('express');

var app = express();

var dateTime = require('node-datetime');

var dt = dateTime.create();

var formatted = dt.format('Y-m-d H:M:S');

console.log(formatted);

app.listen(3000, () => console.log(`App listening on port 3000`))

Ouput

2021-06-17 09:26:37

i hope it can help you…

Leave a Comment