How to date format a field obtained from the database?

2

I get a field from the string type database

field = 20180117

I'm trying to do something similar to this

datetime.strptime(campo, '%Y-%m-%d').strftime('%Y-%m-%d-T%H:%M:%S')

2018-01-17-T00:00:00

Any ideas or suggestions?

    
asked by afr 17.01.2018 в 21:38
source

1 answer

2

Sure, look, with your example:

import datetime

dt=datetime.datetime.strptime("20180117",'%Y%m%d')

print(dt)

Throw:

  

2018-01-17 00:00:00

DEMO

    
answered by 17.01.2018 / 21:51
source