To get the current date in JavaScript you’ll only need the Vanilla JavaScript Date API. All the information about the current date can be accessed in one line of code in various formats.

  1. Get Current Day
  2. Get Current Month
  3. Get Current Year
  4. Get Current Time
  5. Get Current Date

You’ll need to create a new Date object using the Date() constructor to get the current date. The Date constructor is supported in all browsers. Instantiate it without any parameters:

“When no parameters are provided, the newly-created Date object represents the current date and time as of the time of instantiation.”

- MDN

const currentDate = new Date()
Current Date object output
Current Date object output

Get Current Day

Get current week day

To get the current weekday use the getDay() method on the currentDate object. The return value is an integer number.

const currentDate = new Date()
currentDate.getDay()
Current week day output
Current week day output

NOTE: The weekday index starts from 0, and the 0 represents Sunday.

Get current week day name

If you want to get the name of the weekday use the toLocaleString() method with properties. The return value is a string with a language-sensitive representation of this date.

const currentDate = new Date()
currentDate.toLocaleString('en-US', { weekday: 'long' })
Current week day name output
Current week day name output

The first argument is the locale which represents the language. The second is the options object, where you can set the weekday property with a value of long.

Get current day of the month

To get the day of the month use the getDate() method. The return value is an integer number.

const currentDate = new Date()
currentDate.getDate()
Current day of the month output
Current day of the month output

Get Current Month

Get current month number

To get the current month use the getMonth() method. The return value is an integer number.

NOTE: Month index starts from 0, and 0 represents January.

const currentDate = new Date()
currentDate.getMonth()
Current month output
Current month output

Get current month name

To get the name of the month use the toLocaleString() method with properties. The return value is a string with a language-sensitive representation of this date.

const currentDate = new Date()
currentDate.toLocaleString('en-US', { month: 'long' })
Current month name output
Current month name output

The first argument is the locale which represents the language. The second is the options object, where you can set the month property with a value of long.

Get Current Year

To get the current year use the getFullYear() method. The return value is an integer number.

const currentDate = new Date()
currentDate.getFullYear()
Current year output
Current year output

Get Current Time

To get current time you can use one of the following methods.

Get current hours

To get current hours use the getHours() method. The return value is an integer number.

const currentDate = new Date()
currentDate.getHours()
Current hours output
Current hours output

Get current minutes

To get current minutes use the getMinutes() method. The return value is an integer number.

const currentDate = new Date()
currentDate.getMinutes()
Current minutes output
Current minutes output

Get current seconds

To get current seconds use the getSeconds() method. The return value is an integer number.

const currentDate = new Date()
currentDate.getSeconds()
Current seconds output
Current seconds output

Get current time in a specific format

To get the current time in a specific time format (AM/PM or 24H) use the toLocaleTimeString() method with properties. The return value is a string in a specific format.

const currentDate = new Date()
currentDate.toLocaleTimeString('en-US', { hour12: false })
Current time format output
Current time format output

The first argument is the locale which represents the language. The second is the options object, where you can set the desired time format.

Get Current Date

Finally to get the current date in a string format including weekday, month, month day, and a year, use the toLocaleString() method with parameters including the options object containing all of these values.

const currentDate = new Date()
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
currentDate.toLocaleString('en-US', options)
Current date in string format output
Current date in string format output
Tags:
js