Date gives you several methods to set dates and time.
Methods to set Date
setFullYear: Set 4 digit year in Local TimesetMonth: Set month of the year in Local TimesetDate: Set day of the month in Local Time
Note: Each method has its UTC equivalent:
setUTCFullYear: Set 4 digit year in UTCsetUTCMonth: Set month of the year in UTCsetUTCDate: Set day of the month in UTC
Methods to set time
setHours: Set hours in Local TimesetMinutes: Set minutes in Local TimesetSeconds: Set seconds in Local TimesetMilliseconds: Set milliseconds in Local TimesetTime: Sets timestamp
Note: Each method (except setTime) has its equivalent too:
setUTCHours: Set hours in UTCsetUTCMinutes: Set minutes in UTCsetUTCSeconds: Set seconds in UTCsetMilliseconds: Set milliseconds in UTC
Using the set methods
You can use any method here to change a date. Hereβs an example where we change the month from November to December.
const date = new Date(2019, 10)console.log(date) // 01 Nov 2019
date.setMonth(11)console.log(date) // 01 Dec 2019Exercise
Create a Date object and set it to 23 June 2019 at 8pm Local Time.
Do the following:
- Set minutes to 59
- Set hours to 22
- Set date to 15
console.log the new date at each point. Whatβs this new date. Can you explain why you get that result?
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson