Dates and Times in JavaScript
Dates and Times = how to create, change and show dates and times.
This chapter has 2 lessons and 8 exercises you run and check in your browser. Plan on about 47 minutes.
- Date Fundamentals: creating dates, reading their parts, the month rule and date maths.
- Date Formatting and Time Zones:
Intl.DateTimeFormat, locales, time zones and date ranges.
After this chapter, you can:
- Create a date from text, from parts or from a timestamp.
- Read the year, month and day without the month-number mistake.
- Add days, months and years, and count the days between dates.
- Show a date in the customer's language and time zone.
Every lesson has short explanations, code boxes you edit and run in the live editor, and a web page example you can click.
Before you start, finish Functions.
Next chapter: Arrays.
Lessons in this section
- Date Fundamentals
What a Date stores, the current time and timestamps, four ways to create a date, reading parts with local and UTC getters, the zero-based month, rollover, invalid dates, parsing ISO strings, validation, date arithmetic and the setters.
- Explain that a Date stores one number of milliseconds since 1970 in UTC
- Create a date from nothing, a string, parts and a timestamp
- Read date and time parts with the local and the UTC getters
- Apply the zero-based month rule and predict rollover
- Date Formatting and Time Zones
Showing dates with toString, toDateString and toISOString, formatting for people with Intl.DateTimeFormat, locales, dateStyle and timeStyle, the timeZone option and getTimezoneOffset, and filtering and comparing dates in a half-open range.
- Choose between toString, toDateString and toISOString for a given job
- Format a date for people with Intl.DateTimeFormat, a locale, dateStyle and timeStyle
- Show the same moment in any time zone with the timeZone option
- Read getTimezoneOffset and explain its sign