2

For simple formats its easy to come up with equivalents

PHP:

date("Y-m-d") === "2012-07-25";

Python:

date.strftime("%Y-%m-%d") == "2012-07-25"

But what is the Python equivalent of

date("jS F Y") === "25th July 2012";
Mat
  • 6,694
  • 7
  • 35
  • 39

3 Answers3

3

I'm afraid I think you'll have to work the suffix out yourself:

>>> t = time.localtime()
>>> suffix = 'st' if t.tm_mday in [1,21,31] else 'nd' if t.tm_mday in [2, 22] else 'rd' if t.tm_mday in [3, 23] else 'th'
>>> time.strftime('%d%%s %B %Y', t) % suffix
'25th July 2012'

It's a bit English-centric for a programming language feature, so you can see why they didn't include it.

*Edited to add the "rd" suffix for 3rd and 23rd.

MarkH
  • 79
  • 1
  • 8
Rodrigo Queiro
  • 1,324
  • 8
  • 15
  • Yup, Guido van Rossum is dutch – pat34515 Jul 25 '12 at 08:57
  • @Patrick: this has nothing to do with Guido's nationality and everything to do with the C standard strftime routine. See http://linux.die.net/man/3/strftime – Martijn Pieters Jul 25 '12 at 09:02
  • I knew different locales have different date formatting rules, I just thought there might be a Python package that implemented them. In particular it would be nice if there was a format specifier that would output "July 25th 2012" for en-US, "25th July 2012" for en-GB, or "25 Juli 2012" for fr-FR. Guess I'll have to write it myself! – Mat Jul 25 '12 at 09:03
  • @Matt: My answer points you to such a package. Most date formatting does not absolutely need to include `th` and `nd`. – Martijn Pieters Jul 25 '12 at 09:08
  • @MartijnPieters But the date format my British clients require does. I don't think I'll get very far telling them that they can't have what they are paying for because it wouldn't work in Dutch :) – Mat Jul 25 '12 at 09:17
  • @Mat: Sorry, but again, this has nothing to do with Dutch. You can tell them about the Unicode standards though.. – Martijn Pieters Jul 25 '12 at 09:19
  • @Mat: Had to verify this; see https://en.wikipedia.org/wiki/Comparison_of_American_and_British_English#Dates. UK dates can use either ordinal or cardinal numbers, with ordinals being slightly more formal. – Martijn Pieters Jul 25 '12 at 09:34
1

The standard library only supports the standard C library strftime formatting codes, which are rather weak when it comes to localization. %B gives you the full month name in the locale your program is running under; if that's the English locale it'll give you 'April', but it could just as well give you 'Avril' if you are running on a french computer.

For web applications, you really want to use an external library like Babel to do that instead; Babel provides you with extra routines for formatting python datetime instances in different languages:

>>> from babel.dates import format_date
>>> format_date(d, format='long', locale='en')
u'April 1, 2007'

where format='long' is defined as a language-specific pattern.

Babel uses the Unicode Locale Data markup language to define these patterns giving you access to large pre-defined locale libraries that define these patters for you. Note that the grammatically correct way is to use cardinal, not ordinal numbers when formatting dates; British English allows for both ordinal and cardinal dates. As such the Unicode standard does not include ordinal post-fixes for dates.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
-1
import datetime
print datetime.datetime.now().strftime("%dth %B %Y")

For more details see http://docs.python.org/library/datetime.html#strftime-strptime-behavior

rx_tx
  • 190
  • 1
  • 5