python format datetime with “st”, “nd”, “rd”, “th” (english ordinal suffix) like PHP’s “S”

The django.utils.dateformat has a function format that takes two arguments, the first one being the date (a datetime.date [[or datetime.datetime]] instance, where datetime is the module in Python’s standard library), the second one being the format string, and returns the resulting formatted string. The uppercase-S format item (if part of the format string, of course) is the one that expands to the proper one of ‘st’, ‘nd’, ‘rd’ or ‘th’, depending on the day-of-month of the date in question.

Leave a Comment