0

I am retrieving a timezone aware DateTime object from my postgres db.

Now I want to convert this datetime object into it's string representation.

Normally I would do something like this:

str(datetime.datetime(2016, 1, 15, 9, 59, 45, 165904))
'2016-01-15 09:59:45.165904'

But here I have:

datetime.datetime(2016, 1, 15, 9, 59, 45, 165904, tzinfo=<UTC>)

I cannot find a way anywhere to find the str repesentation of the given object.

3
  • Does var_with_timezone.strftime('%c') help? Commented Jan 15, 2016 at 10:14
  • @AlexMorozov can you please post any example? Commented Jan 15, 2016 at 10:16
  • Please check out the lambo477's answer, he's talking about the same thing. Commented Jan 15, 2016 at 10:24

1 Answer 1

2

Here is a simple example of how to convert a datetime object to a string:

import datetime
import pytz

x = datetime.datetime(2016, 1, 15, 9, 59, 45, 165904, tzinfo=pytz.UTC)
print(datetime.datetime.strftime(x, "%d/%m/%Y"))

Output

15/01/2016

Options for the format string to pass into the strftime() function are in the Python documentation.

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.