Zack
Zack

Reputation: 13972

Datetime formatting in python

Should be simple enough. I have a datetime object in python, I need it to be of the form

20131002

Is there a way to format this without having to resort to breaking the date into it's components and putting them together in a string manually?

Upvotes: 0

Views: 124

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

Use datetime.strftime:

>>> from datetime import datetime
>>> dt = datetime.now()
>>> dt.strftime('%Y%m%d')
'20131003'

Upvotes: 1

john_science
john_science

Reputation: 6551

This is super easy in the Python datetime library:

from datetime import datetime

test = datetime.now()     # This is just some test datetime object
test.strftime('%Y%m%d')  # This is the format statement

However, a quick Google search shows me that you would find similar answers by searching for "python datetime format".

Upvotes: 1

Related Questions