Time formatting and date suffixes

J

Jeffrey E. Forcier

This seems like a dead simple question, as it's so rudimentary I
can't believe it hasn't been addressed before. I'm using the
time.strftime() function (actually the mxDateTime implementation, but
they're compatible so it shouldn't matter) to spit out fairly basic
date formats, to wit:

January 25th, 2005

The various and sundry date objects in both mxDateTime and Python
proper's time/datetime don't seem to have anything anywhere dealing
with the 'th' suffix on the date. So in other words, I can use
strftime() to get 'January 25, 2005' but don't see anything dealing
with outputting the suffixes like 'th', 'nd' and the like. Googling
around and searching this list's archives aren't turning anything up,
either.

Am I missing something obvious, or is it just really, really frowned
upon to use such a locale-specific function as English date suffixes?

Thanks,
Jeff

--
Jeffrey E. Forcier
Junior Developer, Research and Development
Stroz Friedberg, LLC
15 Maiden Lane, 12th Floor
New York, NY 10038
[main]212-981-6540 [direct]212-981-6546
http://www.strozllc.com

This message is for the named person's use only. It may contain
confidential, proprietary or legally privileged information. No right to
confidential or privileged treatment of this message is waived or lost
by any error in transmission. If you have received this message in
error, please immediately notify the sender by e-mail or by telephone at
212.981.6540, delete the message and all copies from your system and
destroy any hard copies. You must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message if you are
not the intended recipient.
 
J

John Machin

Jeffrey said:
This seems like a dead simple question, as it's so rudimentary I can't
believe it hasn't been addressed before. I'm using the time.strftime()
function (actually the mxDateTime implementation, but they're
compatible so it shouldn't matter) to spit out fairly basic date
formats, to wit:

January 25th, 2005

The various and sundry date objects in both mxDateTime and Python
proper's time/datetime don't seem to have anything anywhere dealing
with the 'th' suffix on the date. So in other words, I can use
strftime() to get 'January 25, 2005' but don't see anything dealing
with outputting the suffixes like 'th', 'nd' and the like. Googling
around and searching this list's archives aren't turning anything up,
either.

Am I missing something obvious, or is it just really, really frowned
upon to use such a locale-specific function as English date suffixes?

I think the lack of facility is probably due to there being not much
call for that sort of thing -- people doesn't usually go for the "third
day after Michaelmas in the year of our Lord two thousand and five, at
ten o'clock in the forenoon" style these days. If you don't use text,
but stick with the recognisable unambiguous ISO standard format
(2005-01-25), you don't have to worry about locales. However I guess you
have to keep the PHB happy; you can write your own routine in a few lines.

Hint:

if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]

HTH,
John
 
J

Jeffrey E. Forcier

See, I was thinking I'd have to write a short function to do it myself,
but I hadn't realized it could be quite *that* short. Thanks! That'll
do quite nicely, and is definitely something I'm going to throw in my
"general utility functions" folder :)

Regards,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top