How To Get Current Date And Time In Node.js

Websolutionstuff | Sep-01-2021 | Categories : jQuery Node.js

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handled with the Javascript Date object. It is loaded by default and requires no import of modules.

Also you can get current date, month, year, hour, minutes & seconds in Node.js. In Node.js you can get the current date-time formatted as YYYY-MM-DD hh:mm:ss, YYYY-MM-DD, DD-MM-YYYY etc.

And I will share example about how to get the current timestamp and how to get date-time from a given timestamp.

Example 1 :

The current date and time can be fetched by creating a new Date object.

var express = require('express');
var app = express();
  
var date_time = new Date();
console.log(date_time);
  
app.listen(3000);

 

Output : 

2021-08-28T08:39:45.558Z

 

 

Example 2 :

Now in this example we will see different methods and get output in YYYY-MM-DD hh:mm:ss format.

const express = require("express")
const app = express()

let date_time = new Date();

// get current date
// adjust 0 before single digit date
let date = ("0" + date_time.getDate()).slice(-2);

// get current month
let month = ("0" + (date_time.getMonth() + 1)).slice(-2);

// get current year
let year = date_time.getFullYear();

// get current hours
let hours = date_time.getHours();

// get current minutes
let minutes = date_time.getMinutes();

// get current seconds
let seconds = date_time.getSeconds();

// prints date in YYYY-MM-DD format
console.log(year + "-" + month + "-" + date);

// prints date & time in YYYY-MM-DD HH:MM:SS format
console.log(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);

 

Output :

2021-08-28

2021-08-28 14:11:21

 

Example 3 :

Get current timestamp using Date.now() method. Note that this method returns the timestamp in milliseconds. To get the timestamp as seconds we can divide it by 1000.

let timestamp = Date.now();

// timestamp in milliseconds
console.log(timestamp);

// timestamp in seconds
console.log(Math.floor(timestamp/1000));

 

Output :

1630142227639

1630142227

 

 

Example 4 : 

In this example we will get Date and Time from Timestamp. And timestamps are specified as milliseconds.if the given timestamp is in seconds you will need to convert the same to milliseconds by multiplying with 1000.

// current timestamp in milliseconds
let ts = Date.now();

let date_time = new Date(ts);
let date = date_time.getDate();
let month = date_time.getMonth() + 1;
let year = date_time.getFullYear();

// prints date & time in YYYY-MM-DD format
console.log(year + "-" + month + "-" + date);

 

Output : 

2021-08-28

 

 


 

You might also like :

 

Recommended Post
Featured Post
How To Convert Image Into Base64 String Using jQuery
How To Convert Image Into Base...

In this example, we will see how to convert an image into a base64 string using jquery. Base64 encoding schemes are...

Read More

Jan-03-2022

Paypal Payment Gateway Integration In Laravel 8
Paypal Payment Gateway Integra...

In this tutorial, we will see paypal payment gateway integration in laravel 8. Paypal is an international payment p...

Read More

Jan-10-2022

How to Create Custom Facade in Laravel 10
How to Create Custom Facade in...

Laravel, one of the most popular PHP frameworks, provides a powerful feature known as facades. Facades allow you to acce...

Read More

Sep-22-2023

How to Convert UTC Time to Local Time in Laravel 10
How to Convert UTC Time to Loc...

As a Laravel developer, working with time zones is a common requirement, especially when dealing with global application...

Read More

Mar-11-2024