Code Premix

How to Get current Date & Time in JavaScript

πŸ“…July 10, 2021
πŸ—JavaScript

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.

var today = new Date();
// Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time)
  • getFullYear() – Provides current year like 2019.
  • getMonth() – Provides current month values 0-11. Where 0 for Jan and 11 for Dec.
  • getDate() – Provides day of the month values 1-31.
  • getHours() – Provides current hour between 0-23.
  • getMinutes() – Provides current minutes between 0-59.
  • getSeconds() – Provides current seconds between 0-59.
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..!!