ItSolutionStuff.com

How to Get Current Date and Time in Node JS?

By Hardik Savani β€’ June 17, 2021
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...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Create Separate Routes File in Node JS Express?

Read Now β†’
β˜…

How to Get Query String Value in Node.js?

Read Now β†’
β˜…

Node JS Express Route with Parameters Example

Read Now β†’
β˜…

How to Create Route in Node JS Express JS?

Read Now β†’
β˜…

How to Generate 4,6,8,10 Digit Random number in Node JS?

Read Now β†’
β˜…

Node JS Read and Write Json File Example

Read Now β†’
β˜…

Node JS Find and Update Object in Array Example

Read Now β†’
β˜…

How to Get Data from Json File in Node JS?

Read Now β†’
β˜…

How to Remove Element from Array in Node JS?

Read Now β†’
β˜…

How to Push Element in Array in Node JS?

Read Now β†’
β˜…

How to use Underscore JS in Node JS App?

Read Now β†’