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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.