Reputation: 299
How format date in python?
example:
dt = datetime.datetime.strptime("3.3.2017", '%d.%m.%Y')
print '{2}-{1}-{0}'.format(dt.day, dt.month, dt.year)
console return: 2017-3-3
How get date as:
2017-3-3 -- > 2017-03-03
2017-3-13 -- > 2017-03-13
2017-10-10 -- > 2017-10-10
All date must have a length of 10 characters.
Upvotes: 2
Views: 725
Reputation: 6556
Simply try with:
dt = datetime.datetime.strptime("3.3.2017", '%d.%m.%Y')
print '{2}-{1:02d}-{0:02d}'.format(dt.day, dt.month, dt.year)
Output:
2017-03-03
Upvotes: 4
Reputation: 542
Try this:
dt_test = dt.strftime('%Y-%m-%d')
print(dt_test, type(dt_test))
2017-03-03 <class 'str'>
Upvotes: 0