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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Create Separate Routes File in Node JS Express?
- How to Get Query String Value in Node.js?
- Node JS Express Route with Parameters Example
- How to Create Route in Node JS Express JS?
- How to Generate 4,6,8,10 Digit Random number in Node JS?
- Node JS Read and Write Json File Example
- Node JS Find and Update Object in Array Example
- How to Get Data from Json File in Node JS?
- How to Remove Element from Array in Node JS?
- How to Push Element in Array in Node JS?
- How to use Underscore JS in Node JS App?