Trusted answers to developer questions

What is the JavaScript date object?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

The date object is used to handle dates and time in JavaScript.

The object can be instantiated in four ways:

let date = new Date()
let date = new Date(milliseconds)  
let date = new Date(dateString)  
let date = new Date(year, month, day, hrs, min, sec, milliseconds) 

Constructors

  • new Date(): Creates a date object set to the current date and time.
  • new Date(milliseconds): Takes an integer as an argument - which represents milliseconds - and sets the date object to the given number of milliseconds after 1/1/1970. For example, new date(7000) would set the object to 7 seconds after 1/1/1970.
  • new Date(dateString): Takes a string representation of a date, in a format accepted by the date.parse() method, as an argument.
  • new Date(year, month, day, hours, minutes, seconds, milliseconds): Takes arguments in the given order, each specifying a part of the date.

Examples

let date1 = new Date()
console.log("The date is: ", date1)
let date2 = new Date(5000)
console.log("The date is: ", date2)
let date3 = new Date("2019/8/25")
console.log("The date is: ", date3)
let date4 = new Date(2019, 5, 10, 6, 40, 21, 2)
console.log("The date is: ", date4)

Date Methods

Once a date object is instantiated, there are several methods that can be used with it. Some are get methods - which enable you to retrieve specific information from a date, while orders are set methods, which let you edit the information in a date object.

Let’s see some of the important ones in action:

  1. getDay(): Returns an integer which represents the day - 0 means Monday, 1 means Tuesday and so on.
let date = new Date()
console.log("The day is: ", date.getDay())
  1. getFullYear(): Returns an integer that represents the year in the date object.
let date = new Date()
console.log("The year is: ", date.getFullYear())
  1. getHours(): Returns an integer that represents the hours in the date object.
let date = new Date()
console.log("The hours are: ", date.getHours())
  1. getMinutes(): Returns an integer representing minutes in the date object.
let date = new Date()
console.log("The minutes are: ", date.getMinutes())
  1. setFullYear(value): Sets the current year in the date to value.

Note: Do not use the deprecated setYear() anymore.

let date = new Date()
date.setFullYear(2015)
console.log("The edited date with year 2015:", date)
  1. setMonth(value): Takes an integer as an argument which represents the month and changes the month in the date object to value.

Other methods such as setDay(value), setHours(value), setMinutes(value), and setSeconds(value) are used similarly.

Note: Numbering for setMonth() and setDay() starts from 0, so 0 means January, 1 means February and so on…

let date = new Date()
date.setMonth(1) // Remember, 1 means February.
console.log("The edited date with February is: ", date)

RELATED TAGS

javascript
date
object
js
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?