If I ask you whatβs the date today, I donβt expect a full-blown reply that says Tue Jan 23 2019 17:21:13 GMT+0800 (Singapore Standard Time). Iβll expect something along the lines of 23 Jan, 2019.
How do you get 23 Jan, 2019 from the Date object?
Built-in formats
Date comes with seven methods that lets you convert a Date object into a string. Hereβs what each of them do:
toStringreturns:Wed Jan 23 2019 17:23:42 GMT+0800 (Singapore Standard Time)toDateStringreturns:Wed Jan 23 2019toLocaleStringreturns:23/01/2019, 17:23:42toLocaleDateStringreturns:23/01/2019toGMTStringreturns:Wed, 23 Jan 2019 09:23:42 GMTtoUTCStringreturns:Wed, 23 Jan 2019 09:23:42 GMTtoISOStringreturns:2019-01-23T09:23:42.079Z
Each method gives a specific date/time string. Unfortunately, none of them gives us the format (23 Jan, 2019) we wanted.
We need to create the format ourselves with some Date methods.
Date methods
Hereβs a list of methods that helps you format a date.
getFullYear: Gets 4-digit year according to local time.getMonth: Gets month of the year (0-11) according to local time. Month is zero-indexed (which meansJanuary === 0andDecember === 11).getDate: Gets day of the month (1-31) according to local time.getDay: Gets day of the week (0-6) according to local time. Day of the week begins with Sunday (0) and ends with Saturday (6).
So, what should you write to get 23 January, 2019?
Letβs work it through.
Creating a custom format
First, we need to create a date object.
const today = new Date()From this point onwards, weβre going to assume the today is 23 January 2019. Substitute the values for today with the actual date when you work through this lesson.
Getting the day of the month (23) is simple. We can use getDate.
const day = today.getDate()console.log(day) // 23Getting the full year (2019) is simple too. We can use getFullYear.
const year = today.getFullYear()console.log(year) // 2019Getting January is harder.
We only have access to getMonth, and getMonth returns a number from 0 to 11. This means we must convert the month number (0-11) into a month string (January-December) ourselves.
First, we need to create an object that tells us 0 is January, 1 is February, 2 is March⦠and 11 is December.
const monthsInAYear = { 0: 'January', 1: 'February', 2: 'March', 3: 'April', 4: 'May', 5: 'June', 6: 'July', 7: 'August', 8: 'September', 9: 'October', 10: 'November', 11: 'December',}Notice this is a zero-indexed object? This object has the same structure as an Array. We can use an Array instead.
const monthsInAYear = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',]To get January, we use getMonth to get monthNumber from the Date object. Then, we pass monthNumber into monthsInAYear to get January.
const monthIndex = today.getMonth()const monthName = monthsInAYear[monthIndex]console.log(monthName) // JanuaryThe condensed version:
const month = monthsInAYear[today.getMonth()]console.log(month) // JanuaryTo get 23 January, 2019, we put the values we retrieved together in a template literal:
const dateString = `${day} ${month}, ${year}`console.log(dateString) // 23 January, 2019What if you want Jan instead of January?
If you want Jan (shortname) instead of January (longname), you have to modify monthsInAYear to include both shortnames and longnames.
const monthsInAYear = [ { shortname: 'Jan', longname: 'January' }, { shortname: 'Feb', longname: 'February' }, { shortname: 'Mar', longname: 'March' }, { shortname: 'Apr', longname: 'April' }, { shortname: 'May', longname: 'May' }, { shortname: 'Jun', longname: 'June' }, { shortname: 'Jul', longname: 'July' }, { shortname: 'Aug', longname: 'August' }, { shortname: 'Sep', longname: 'September' }, { shortname: 'Oct', longname: 'October' }, { shortname: 'Nov', longname: 'November' }, { shortname: 'Dec', longname: 'December' },]With the new monthsInAYear array, you get January with this:
const longMonth = monthsInAYear[today.getMonth()].longnameconsole.log(longMonth) // JanuaryAnd Jan this way:
const shortMonth = monthsInAYear[today.getMonth()].shortnameconsole.log(shortMonth) // JanGetting day of the week
What if you wanted Wednesday, 23 January instead?
The Date object has a getDay method that tells us the day of the week. It returns a number from 0 to 6. The values goes like this:
0: Sunday1: Monday2: Tuesday3: Wednesday4: Thursday5: Friday6: Saturday
If we want to get Wednesday from Date we need to create a daysInAWeek array that converts numbers into strings.
const daysInAWeek = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',]To get Wednesday we use the following:
const dayString = daysInAWeek[today.getDay()]console.log(dayString) // WednesdayIf you want Wed you know what to do:
const daysInAWeek = [ { shortname: 'Sun', longname: 'Sunday' }, { shortname: 'Mon', longname: 'Monday' }, { shortname: 'Tue', longname: 'Tuesday' }, { shortname: 'Wed', longname: 'Wednesday' }, { shortname: 'Thu', longname: 'Thursday' }, { shortname: 'Fri', longname: 'Friday' }, { shortname: 'Sat', longname: 'Saturday' },]
const dayShortString = daysInAWeek[today.getDay()].shortname // Wedconst dayLongString = daysInAWeek[today.getDay()].longname // WednesdayExercise
Assuming today is 23 January 2019, get the following date strings:
23/01/201901/23/201923 Jan, 201923 Jan, WedWednesday, 23 January, 201923rd January, 2019
Answers
Answer for 23/01/2019:
const today = new Date()const dateString = `${today.getDate()}/${ today.getMonth() + 1}/${today.getFullYear()}`Answer for 01/23/2019:
const today = new Date()const dateString = `${ today.getMonth() + 1}/${today.getDate()}/${today.getFullYear()}`Answer for 23 Jan, 2019:
const monthsInAYear = [ { shortname: 'Jan', longname: 'January' }, { shortname: 'Feb', longname: 'February' }, { shortname: 'Mar', longname: 'March' }, { shortname: 'Apr', longname: 'April' }, { shortname: 'May', longname: 'May' }, { shortname: 'Jun', longname: 'June' }, { shortname: 'Jul', longname: 'July' }, { shortname: 'Aug', longname: 'August' }, { shortname: 'Sep', longname: 'September' }, { shortname: 'Oct', longname: 'October' }, { shortname: 'Nov', longname: 'November' }, { shortname: 'Dec', longname: 'December' },]
const today = new Date()const day = today.getDate()const month = monthsInAYear[today.getMonth()].shortnameconst year = today.getFullYear()const dateString = `${day} ${month}, ${year}`Answer for 23 Jan, Wed:
const monthsInAYear = [ { shortname: 'Jan', longname: 'January' }, { shortname: 'Feb', longname: 'February' }, { shortname: 'Mar', longname: 'March' }, { shortname: 'Apr', longname: 'April' }, { shortname: 'May', longname: 'May' }, { shortname: 'Jun', longname: 'June' }, { shortname: 'Jul', longname: 'July' }, { shortname: 'Aug', longname: 'August' }, { shortname: 'Sep', longname: 'September' }, { shortname: 'Oct', longname: 'October' }, { shortname: 'Nov', longname: 'November' }, { shortname: 'Dec', longname: 'December' },]
const daysInAWeek = [ { shortname: 'Sun', longname: 'Sunday' }, { shortname: 'Mon', longname: 'Monday' }, { shortname: 'Tue', longname: 'Tuesday' }, { shortname: 'Wed', longname: 'Wednesday' }, { shortname: 'Thu', longname: 'Thursday' }, { shortname: 'Fri', longname: 'Friday' }, { shortname: 'Sat', longname: 'Saturday' },]
const today = new Date()const day = today.getDate()const month = monthsInAYear[today.getMonth()].shortnameconst dayString = daysInAWeek[today.getDay()].shortnameconst dateString = `${day} ${month}, ${dayString}`Answer for Wednesday, 23 January, 2019:
const monthsInAYear = [ { shortname: 'Jan', longname: 'January' }, { shortname: 'Feb', longname: 'February' }, { shortname: 'Mar', longname: 'March' }, { shortname: 'Apr', longname: 'April' }, { shortname: 'May', longname: 'May' }, { shortname: 'Jun', longname: 'June' }, { shortname: 'Jul', longname: 'July' }, { shortname: 'Aug', longname: 'August' }, { shortname: 'Sep', longname: 'September' }, { shortname: 'Oct', longname: 'October' }, { shortname: 'Nov', longname: 'November' }, { shortname: 'Dec', longname: 'December' },]
const daysInAWeek = [ { shortname: 'Sun', longname: 'Sunday' }, { shortname: 'Mon', longname: 'Monday' }, { shortname: 'Tue', longname: 'Tuesday' }, { shortname: 'Wed', longname: 'Wednesday' }, { shortname: 'Thu', longname: 'Thursday' }, { shortname: 'Fri', longname: 'Friday' }, { shortname: 'Sat', longname: 'Saturday' },]
const today = new Date()const day = today.getDate()const month = monthsInAYear[today.getMonth()].longnameconst dayString = daysInAWeek[today.getDay()].longnameconst year = today.getFullYear()const dateString = `${dayString}, ${day} ${month}, ${year}`Answer for 23rd January, 2019:
This one is trickier. You need to create the day suffix for rd too. Hereβs the code:
const monthsInAYear = [ { shortname: 'Jan', longname: 'January' }, { shortname: 'Feb', longname: 'February' }, { shortname: 'Mar', longname: 'March' }, { shortname: 'Apr', longname: 'April' }, { shortname: 'May', longname: 'May' }, { shortname: 'Jun', longname: 'June' }, { shortname: 'Jul', longname: 'July' }, { shortname: 'Aug', longname: 'August' }, { shortname: 'Sep', longname: 'September' }, { shortname: 'Oct', longname: 'October' }, { shortname: 'Nov', longname: 'November' }, { shortname: 'Dec', longname: 'December' },]
const dateSuffix = { 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th', 10: 'th', 11: 'th', 12: 'th', 13: 'th', 14: 'th', 15: 'th', 16: 'th', 17: 'th', 18: 'th', 19: 'th', 20: 'th', 21: 'st', 22: 'nd', 23: 'rd', 24: 'th', 25: 'th', 26: 'th', 27: 'th', 28: 'th', 29: 'th', 30: 'th', 31: `st`,}
const today = new Date()const day = today.getDate()const daySuffix = dateSuffix[day]const month = monthsInAYear[today.getMonth()].longnameconst year = today.getFullYear()const dateString = `${day}${daySuffix} ${month}, ${year}`Why do we use an object for dateSuffix? Simple, well, you can use an array too. But can you keep track of the correct number of ths without the numeric key?
Use what makes sense! :)
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