Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Ordinal Output?

Tags:

python

I'm wondering if there is a quick and easy way to output ordinals given a number in python.

For example, given the number 1, I'd like to output "1st", the number 2, "2nd", et cetera, et cetera.

This is for working with dates in a breadcrumb trail

Home >  Venues >  Bar Academy >  2009 >  April >  01  

is what is currently shown

I'd like to have something along the lines of

Home >  Venues >  Bar Academy >  2009 >  April >  1st 
like image 463
Mez Avatar asked Apr 11 '09 00:04

Mez


People also ask

How do you write ordinal date?

When writing ordinal numbers such as 1st, 2nd, 3rd, etc. you should use the last two letters on the word as it would be if you wrote out the whole word. Below are the ordinal numbers both written out and with digits for 1-20. As you can see, 1st, 2nd, and 3rd use -st, -nd, and -rd, but 4th-20th use -th.

Are dates cardinal or ordinal numbers?

How to Write the Month and Day. When referring to a specific date in the month-day date format, use cardinal numbers (one, two, three) rather than ordinal numbers (first, second, third). This may feel counterintuitive because we normally use ordinal numbers when we “speak” of dates.

How do you find ordinal dates in Excel?

Select a cell that used to place the ordinal date, click Kutools > Formula Helper > Date & Time > Convert date to ordinal date.

What is ordinal date Python?

toordinal() is a simple method used to manipulate the objects of DateTime class. It returns proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. The function returns the ordinal value for the given DateTime object.


1 Answers

Or shorten David's answer with:

if 4 <= day <= 20 or 24 <= day <= 30:     suffix = "th" else:     suffix = ["st", "nd", "rd"][day % 10 - 1] 
like image 185
Abizern Avatar answered Sep 17 '22 23:09

Abizern