Created
July 2, 2020 21:42
-
-
Save andreiskandar/bd95c0db8e8606780c8db0634ac16688 to your computer and use it in GitHub Desktop.
Create a function named talkingCalendar format YYYY/MM/DD, and returns a new human readable date that looks like December 2nd, 2017.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function named talkingCalendar that takes in a date string with the format YYYY/MM/DD, | |
and returns a new human readable date that looks like December 2nd, 2017. | |
*/ | |
const getMonthName = (month) => { | |
const monthName ={ | |
01: 'January', 02: 'February', 03: 'March', | |
04: 'April', 05: 'May', 06: 'June', | |
07: 'July', 08: 'August', 09: 'September', | |
10: 'October', 11: 'November', 12: 'December' | |
}; | |
return monthName[month]; | |
} | |
const getDay = (day) => { | |
switch(day){ | |
case '01': | |
day = '1st'; | |
break; | |
case '02': | |
day = '2nd'; | |
break; | |
case '03': | |
day = '3rd'; | |
break; | |
case '11': case '21': case '31': | |
day += 'st'; | |
break; | |
case '12': case '22': | |
day += 'nd'; | |
break; | |
case '13': case '23': | |
day += 'rd'; | |
break; | |
default: | |
day += 'th'; | |
break; | |
} | |
return day; | |
} | |
const talkingCalendar = function(date) { | |
const dateSplit = date.split('/'); | |
const year = dateSplit[0]; | |
const month = dateSplit[1]; | |
const day = dateSplit[2]; | |
return `${getMonthName(month)} ${getDay(day)}, ${year}`; | |
} | |
console.log(talkingCalendar("2017/12/03")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment