Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates

Ever written a giant case statement to add “-st”, “-nd”, “-rd”, “-th” to display dates like “March 7th” or “September 1st”?

Converting numbers like 1, 2, 7, 19 into “1st”, “2nd”, “7th”, “19th” is called ordinalizing.

ordinalize(number)
Turns a numbe…


This content originally appeared on DEV Community and was authored by matt swanson

Ever written a giant case statement to add "-st", "-nd", "-rd", "-th" to display dates like "March 7th" or "September 1st"?

Converting numbers like 1, 2, 7, 19 into "1st", "2nd", "7th", "19th" is called ordinalizing.

ordinalize(number)
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Instead of manually add special cases, check if your language, library, or framework has a helper built-in.

For example, in Rails, you can call the ordinalize method on a number or use the long_ordinal date format.

> 3.ordinalize
=> "3rd"

> Date.today.to_s(:long_ordinal)
=> "September 29th, 2021"

Happy hacking!


This content originally appeared on DEV Community and was authored by matt swanson


Print Share Comment Cite Upload Translate Updates
APA

matt swanson | Sciencx (2021-09-29T16:44:29+00:00) Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates. Retrieved from https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/

MLA
" » Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates." matt swanson | Sciencx - Wednesday September 29, 2021, https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/
HARVARD
matt swanson | Sciencx Wednesday September 29, 2021 » Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates., viewed ,<https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/>
VANCOUVER
matt swanson | Sciencx - » Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/
CHICAGO
" » Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates." matt swanson | Sciencx - Accessed . https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/
IEEE
" » Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates." matt swanson | Sciencx [Online]. Available: https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/. [Accessed: ]
rf:citation
» Ordinalize: Adding “-st”, “-nd”, “-rd”, “-th” to dates | matt swanson | Sciencx | https://www.scien.cx/2021/09/29/ordinalize-adding-st-nd-rd-th-to-dates/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.