2

Possible Duplicate:
How to print date in a regular format in Python?

I would like to know how to convert the following date to natural language, including time zone in python?

input:

"'2012-09-27T02:00:00Z'"

expected output:

Wednesday, September 26 of 2012 Mountain Time

Thanks in advance!

Note Edit: So far I tried django humanize, although it doesn't handle very well complex date-time strings.

Solution:

Thanks for all the information. I ended up parsing the original string and using pitz and strftime like this:

    my_date = '2012-09-27T02:00:00Z'
    utc_date_object = datetime(int(my_date[0:4]), int(my_date[5:7]), int(my_date[8:10]),int(my_date[11:13]),int(my_date[14:16]),int(my_date[17:19]),0,pytz.utc)
    mt_date_object = utc_date_object.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('US/Mountain'))
    natural_date = mt_date_object.strftime("%A, %B %d of %Y")

Output:

'Wednesday, September 26 of 2012'
Community
  • 1
  • 1
ipegasus
  • 14,796
  • 9
  • 52
  • 76

4 Answers4

3

The Babel project offers a full-featured date and time localization library.

You'll also need the iso8601 module to parse a date-time string with a timezone correctly.

It either formats dates and times based on locale:

>>> from datetime import date, datetime, time
>>> from babel.dates import format_date, format_datetime, format_time
>>> d = date(2007, 4, 1)
>>> format_date(d, locale='en')
u'Apr 1, 2007'
>>> format_date(d, locale='de_DE')
u'01.04.2007'

or it let's you specify the format in detail. This includes formatting the timezone.

Putting the parser and the formatter together:

>>> dt = iso8601.parse_date("2012-08-25T02:00:00Z")
>>> format_date(dt, "MMMM dd, yyyy", locale='en') + ' at ' + format_time(dt, "HH:mm V")
u'August 25, 2012 at 02:00 World (GMT) Time'

Ordinals ('1st', '2nd', etc.) are a little harder to do internationally, and the LDML format used by Babel doesn't include a pattern for these.

If you must have an ordinal in your date formatting (perhaps because you only expect to output in English), you'll have to create those yourself:

>>> suffix = ('st' if dt.day in [1,21,31]
...                else 'nd' if dt.day in [2, 22] 
...                else 'rd' if dt.day in [3, 23]
...                else 'th')
>>> u'{date}{suffix}, {year} at {time}'.format(
...     date=format_date(dt, "MMMM dd", locale='en'),
...     suffix=suffix, year=dt.year,
...     time=format_time(dt, "HH:mm V"))
u'August 25th, 2012 at 02:00 World (GMT) Time'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

You can get a custom string representation of your date using the strftime() method. strftime accepts a string pattern explaining how you want to format your date.

For example:

print today.strftime('We are the %d, %h %Y')
'We are the 22, Nov 2008'

All the letters after a "%" represent a format for something:

  • %d is the day number
  • %m is the month number
  • %y is the year last two digits
  • %Y is the all year

https://stackoverflow.com/a/311655

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I'm not sure why you asked how the question I posted was relevant and then linked to the same question. Were you saying I should've explained it? – Keith Smiley Nov 12 '12 at 18:20
0

Not 100% the answer to your question, but this code might help you starting formatting time and date:

import datetime
print datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')
mawueth
  • 2,668
  • 1
  • 14
  • 12
0
def myFormat(dtime):
    if dtime.day in [1,21,31] : ending = "st"
    elif dtime.day in [2,22] : ending = "nd" 
    elif dtime.day in [3,23] : ending = "rd" 
    else : ending = "th"

    return dtime.strftime("%B %d"+ ending + " of %Y")   
reptilicus
  • 10,290
  • 6
  • 55
  • 79