• Post category:JavaScript
  • Reading time:3 mins read

Today we will show you how to get current Date & Time in JavaScript. We will also show you how to get a current year, month, date, hours, minutes, seconds, etc.

Let’s get a current date and time by running the following command.

var today = new Date();
// Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time)

Refer following code to get an appropriate output.

var today = new Date();
// Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time)

today.getFullYear();
// Output: 2019

today.getMonth();
// Output: 11

today.getDate();
// Output: 22

today.getDay();
// Output: 0

today.getHours();
// Output: 13

today.getMinutes();
// Output: 18

today.getSeconds();
// Output: 1

today.getMilliseconds();
// Output: 114

today.getTime();
// Output: 1577000881114 (for GMT)

Using the below script we can also get the current date and time in Y-m-d H:i:s format.

var today = new Date(); // Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time)
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;

// Output: 2019-12-22 13:18:1

I hope you find this article helpful.
Thank you for reading. Happy Coding..!!

Leave a Reply