Setting a date with Date methods
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Setting a date with Date methods
Date gives you several methods to set dates and time.
Methods to set Date
setFullYear: Set 4 digit year in Local Time
setMonth: Set month of the year in Local Time
setDate: Set day of the month in Local Time
Note: Each method has its UTC equivalent:
setUTCFullYear: Set 4 digit year in UTC
setUTCMonth: Set month of the year in UTC
setUTCDate: Set day of the month in UTC
Methods to set time
setHours: Set hours in Local Time
setMinutes: Set minutes in Local Time
setSeconds: Set seconds in Local Time
setMilliseconds: Set milliseconds in Local Time
setTime: Sets timestamp
Note: Each method (except setTime) has its equivalent too:
setUTCHours: Set hours in UTC
setUTCMinutes: Set minutes in UTC
setUTCSeconds: Set seconds in UTC
setMilliseconds: 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 2019
Exercise
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?