Skip to content

Instantly share code, notes, and snippets.

@thathurtabit
Last active August 5, 2023 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thathurtabit/160f4197ea87c987c0caa6fd6d64e39b to your computer and use it in GitHub Desktop.
Save thathurtabit/160f4197ea87c987c0caa6fd6d64e39b to your computer and use it in GitHub Desktop.
Get Number Suffix (-st, -nd, -rd, -th)
export const getNUmberSuffix = (number: number): string => {
const thExceptions = (number: number) =>
[11, 12, 13].some((exception) => exception === number);
const lastDigit = number % 10;
if (thExceptions(number) || lastDigit === 0 || lastDigit > 3)
return `${number}th`;
if (lastDigit === 1) return `${number}st`;
if (lastDigit === 2) return `${number}nd`;
return `${number}rd`;
};
@thathurtabit
Copy link
Author

thathurtabit commented Mar 14, 2021

Log out to view:

const logOutNumber = new Array(31).fill(0).forEach((_,index) => console.log(getNumberSuffix(index + 1)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment