Mat.S
Mat.S

Reputation: 1854

Error in changing string to datetime object

So I have test_date1 = "2017-07-21-10-20" and I want to convert to day1 = datetime.strptime(test_date1, "%y-%m-%d-%h-%M") but I get the following error :

time data '2017-07-21-10-20' does not match format '%y-%m-%d-%H-%M'

What are the proper fields for hours and minutes?

Upvotes: 0

Views: 54

Answers (3)

pokeymond
pokeymond

Reputation: 671

Use capital Y for "century" year.

datetime.strptime(test_date1, "%Y-%m-%d-%H-%M")

Reference: http://strftime.org/

%Y Year with century as a decimal number. 2013

Upvotes: -1

tayfun
tayfun

Reputation: 3135

Use capital H for hour. Check this:

datetime.strptime(test_date1, "%Y-%m-%d-%H-%M")

See https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Upvotes: 2

Jay Parikh
Jay Parikh

Reputation: 2489

below code is working

datetime.strptime("2017-07-21-10-20",'%Y-%m-%d-%H-%M')

Upvotes: 1

Related Questions