A Date to Read
Nobody writes "2031, month 4, day 14" on a birthday card. We write "Wednesday, May 14" in words, the way people read dates. Putting a date's numbers together yourself is a lot of work. Dates have methods that write them for people.
toLocaleDateString
toLocaleDateString gives the date as a text, the way people of a language write it. The first thing in its brackets is the language, as a short code. "en-US" is English as it's written in the United States:
const day = new Date();
console.log(day.toLocaleDateString("en-US"));
On 14 May 2031, it shows:
5/14/2031
It's short, with the month first, like Americans write it. "en-GB", English as written in Britain, gives 14/05/2031, with the day first.
Choosing the parts
The second thing in the brackets is an object that says which parts to write, and how:
console.log(
day.toLocaleDateString("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
}),
);
Wednesday, May 14, 2031
Each key is a part of the date, and its value says how to write it. "long" is the whole word (Wednesday, May), "short" is a short one (Wed, May), and "numeric" is a number. A part you leave out isn't written. The browser puts in the commas and the order, as the language wants.
toLocaleTimeString
toLocaleTimeString does the same for the time:
console.log(day.toLocaleTimeString("en-US"));
console.log(day.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit" }));
At half past nine in the morning, it shows:
9:30:00 AM
9:30 AM
"2-digit" writes a number with at least two digits: 9:05, never 9:5. In American English, the hours go from 1 to 12, with AM before noon (12:00 in the day) and PM after it.
Break it on purpose
Build the time yourself: `${day.getHours()}:${day.getMinutes()}`. At 9:05, you get 9:5: getMinutes gives the number 5, with no 0 before it. And in the evening, it says 21:5, not 9:05 PM. Let toLocaleTimeString do it: it knows how each language writes a time.
Mission: Today's date
What you will see: on Wednesday 14 May 2031 at 9:30 in the morning, under the greeting, it says:
Wednesday, May 14, 2031
Last update: 9:30 AM
Under the greeting, the dashboard should show today's date in words, and the time it was last updated.
- Under
#greeting, add a paragraph with the idtoday-date, then one with the idlast-update. #today-dateshows today's date the way American English writes it in full: the day of the week, the month in words, the day and the year, like in the example.#last-updatesaysLast update:, then the time the way American English writes it: the hour, the minutes in two digits, and AM or PM.