0

In Python how do I convert a datetime object like datetime.datetime(2010, 12, 18, 22, 21, 45, 277000) to something like "Sun Dec 18 2010 22:21:45 GMT+0000 (UTC)"?

2
  • 2
    There's a trick to this. It's called search. If you search for "formatting datetime" you'll find hundreds of copies of your exact question. Commented Jul 27, 2011 at 14:35
  • possible duplicate of Python, Convert 9 tuple UTC date to MySQL datetime format Commented Jul 27, 2011 at 14:36

1 Answer 1

3

You can use strftime for this purpose. Read more here.

Not sure about utc offset, but seems that you can use something like this:

import datetime
d = datetime.datetime(2010, 12, 18, 22, 21, 45, 277000)
d.strftime("%a %b %d %Y %H:%M:%S %z")
>>> Sat Dec 18 2010 22:21:45

Where using the link above:

  • %a - Locale’s abbreviated weekday name

  • %b - Locale’s abbreviated month name

  • %d - Day of the month as a decimal number [01,31]

  • %H - Hour (24-hour clock) as a decimal number [00,23]

  • %M - Minute as a decimal number [00,59]

  • %S - Second as a decimal number [00,61]

  • %z - UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.