views:

251

answers:

1

I would like a datetime in python to output like this:

Thu the 2nd at 4:30

But I find no way in python to output st, nd, rd, or th like I can with PHP datetime format with the S string (What they call "English Ordinal Suffix") (http://uk.php.net/manual/en/function.date.php).

Is there a built-in way to do this in python? strftime isn't good enough (http://docs.python.org/library/datetime.html#strftime-strptime-behavior).

I am actually using django and apparently they have a filter which does what I want, but I want a function, not a filter, to do what I want. If a django function exists, then that works for me as well.

+3  A: 

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.

Alex Martelli