1

This is the query i am trying to run in a python file

cur.execute(f"(select data_end_time from table;))
records = cur.fetchall()")
print(records[0])

This is the Output i am getting

datetime.datetime(2020, 7, 6, 17, 0)

Expected output

2020-07-06 17:00:00

records[0] contains value datetime.datetime(2020, 7, 6, 17, 0) and is of type tuple

5
  • This doesnt answer my question .. Commented Jul 6, 2020 at 10:59
  • records[0] contains value datetime.datetime(2020, 7, 6, 17, 0),and is of type tuple Commented Jul 6, 2020 at 11:00
  • Please clarify: do you mean it's a tuple with a datetime object inside? Commented Jul 6, 2020 at 11:02
  • records[0] is of type tuple with datetime inside it Commented Jul 6, 2020 at 11:05
  • did you try records[0][0].strftime('%Y-%m-%d %H:%M:%S')? Commented Jul 6, 2020 at 11:05

1 Answer 1

0

You have to use datetime.strftime for converting datetime object into string

import datetime

x = datetime.datetime(2020, 7, 6, 17, 0)

print(x.strftime('%Y-%m-%d %H:%M:%S')

#output
'2020-07-06 17:00:00'
Sign up to request clarification or add additional context in comments.

3 Comments

Din work ..Error :'tuple' object has no attribute 'strftime'
try x[0]..strftime('%Y-%m-%d %H:%M:%S')
records[0][0].strftime('%Y-%m-%d %H:%M:%S') worked ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.