Quiz: 14 JavaScript Date Questions
Learn to impress at parties with JS trivia! ✨
How well do you know the Date class?
- Prove your JavaScript skillz! 🚀
- No login or signup required. ✨
- Multiple choice. 🤖 … How difficult can it be, eh?
Outline
The Date class in JavaScript has a notoriously difficult API. It was inherited from Java, and I can only assume was inspired ancient neolithic time keeping methods.
The struggle to get Date’s leads many developers to use 3rd party libraries without question. While often a safe and reliable choice, these libraries are rarely needed for formatting dates or localization!
This quiz is designed to test (and deepen) your knowledge of the native Date API. Use the green buttons for hints & explanations! Hopefully by the end of the challenge you’ll have solidified your understanding of Date’s in JavaScript.
NOTE: Assume all examples assume GMT-7 local timezone.
👇 14 Questions Below 👇
Which local calendar date does this object represent? Console display formatting may vary.
const d1 = new Date(2020, 1, 1)console.log(d1)The Month argument is zero-based. With a range of 0-11 (using western calendars.)
‘February’ has an index value of one. (Think of it like an array lookup.)
Which local calendar date does this object represent? Console display formatting may vary.
const d2 = new Date(2020, 0, 1)console.log(d2)The Month argument is zero-based. With a range of 0-11 (using western calendars.)
‘January’ has an index value of zero. (Think of it like an array lookup.)
What will the output include?
const d3 = Date('Thu, 01 Jan 1970 00:00:00 GMT')console.log(d3)Calling Date() without new ignores all arguments and returns a string for the current date and time. Use new Date(...) to create a Date object from the supplied arguments.
Assume the local timezone is fixed at GMT−7.
What will the output include?
const date = new Date(2020)console.log(date.getFullYear())A Date instance created with a single integer argument is interpreted as a Unix Epoch value. Epoch is a count of milliseconds since Jan 1st, 1970.
A value of 2020 (milliseconds) translates to 2 seconds after Jan 1st, 1970.
Then since our local time zone is a negative offset of -7 hours, we end up with Wed Dec 31 1969 17:00:02 GMT-0700 (Mountain Standard Time).
You can side-step the local timezone offset by using .getUTCFullYear().
What will the output include?
var date = Date.UTC('2020-01-02T00:00')console.log(date.toUTCString())Date.UTC() expects numeric year/month/etc. arguments. This string cannot be converted to a valid year, so the call first returns NaN. That is a number, not a Date object; calling its .toUTCString() throws TypeError.
What will the output include?
const d = Date.UTC(2020, 0, 1)console.log(d)The helper method Date.UTC doesn’t return a date instance. It returns an integer in milliseconds.
Which local calendar date does this object represent? Console display formatting may vary.
const d = new Date(2020, 0, 1) d.setDate(1) console.log(d)The .setDate() method sets the day of the month, based on the given instance’s current month.
If a value is provided outside of the number of days available, the date instance month value will be adjusted (e.g. A setDate(32) in January will calculate as February 1st.)
Which local calendar date does this object represent? Console display formatting may vary.
const d = new Date(2020, 0, 1) d.setMonth(1) console.log(d)The .setMonth() method sets the month of the given date instance.
The month argument is zero-based, with a range of 0-11 (using western calendars.)
Which local calendar date does this object represent? Console display formatting may vary.
const d = new Date(2020, 0, 1) d.setMonth(12) console.log(d)The .setMonth() method sets the month of the given date instance.
The month argument is zero-based, with 12 values in the range of 0-11 (using western calendars.)
Here we see the year is adjusted to 2021, because setMonth(12) is 1 more than 11 (December).
Which local calendar date does this object represent? Console display formatting may vary.
const d = new Date(2020, 0, 1) d.setMonth(13) console.log(d)The .setMonth() method sets the month of the given date instance.
The month argument is zero-based, with a range of 0-11 (using western calendars.)
Here we see the month and year is adjusted to February 2021, because setMonth(13) is 2 more than 11 (December).
Which local calendar date does this object represent? Console display formatting may vary.
const d = new Date(2020, 0, 1) d.setMonth(-1) console.log(d)The .setMonth() method sets the month of the given date instance.
The month argument is zero-based, with a range of 0-11 (using western calendars.)
Here we see the month and year rolls back to December 2019, because setMonth(-1) is less than 0 (January).
