0

I'm trying to convert a string into a datetime object...

time = '10:00:00'
date = '2016-10-03'
date = date + ' ' + time
print date
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%y')
print date

this prints out:

2016-10-03 10:00:00
2000-10-03 10:00:00

for some reason it changes the year date..

when I do:

date = '2016-10-03'
date = datetime.strptime(date, '%Y-%m-%d')
print date

It works correct and I get:

2016-10-03

How come when I add the time in there it changes the year?

Thanks

1
  • 1
    The %y at the end should be a %S --> seconds not year in the time. @McGrady beat me to it! Cheers! Commented Feb 20, 2017 at 10:41

1 Answer 1

5

Try this:

time = '10:00:00'                                       
date = '2016-10-03'                                     
date = date + ' ' + time                                
print date                                              
print datetime.strptime(date, '%Y-%m-%d %H:%M:%S')      

%y Year without century as a zero-padded decimal number.
%Y Year with century as a decimal number.

It seems that you just made a misspelling.Maybe what you want is %Y-%m-%d %H:%M:%S'.

See more details from datetime Python.Hope this helps.

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

1 Comment

@nrs90 if this was the answer can you please accept it? gives the answerer more rep and then this question is marked as solved

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.