1

I want to convert my string start_date='2013-12-01' to datetime.date(2013, 12, 1).

Was trying to do it like this:

datetime.strptime(start_date,'%Y-%M-%d')

But it gave me output:

datetime.datetime(2013, 1, 1, 0, 12)

How can I get an output like datetime.date(2013, 12, 1)?

1 Answer 1

4

Use:

>>> datetime.strptime(start_date, '%Y-%m-%d')
datetime.datetime(2013, 12, 1, 0, 0)

If you just want a date object:

>>> datetime.strptime(start_date, '%Y-%m-%d').date()
datetime.date(2013, 12, 1)

%M is minute; %m is month; see the strptime docs for more information. Note that I'm assuming you've done from datetime import datetime or similar here.

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

1 Comment

Expected result should in the format as datetime.date(2013, 12, 1)

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.