0

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?

3
  • 1
    Have you looked at datetime.strftime? Commented Oct 2, 2013 at 18:51
  • thanks. That was easy. Would you reccomend deleting this question? The question DID get answered, but now it almost seems trivial. Commented Oct 2, 2013 at 18:53
  • I've posted that as an answer. Commented Oct 2, 2013 at 18:54

2 Answers 2

1

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".

Sign up to request clarification or add additional context in comments.

Comments

1

Use datetime.strftime:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.