Reputation: 63
Hello I am trying to convert a string which is a = "2019-04-22 00:00"
to a datetime but it does not work, I tried this :
a = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M')
But I got
time data 'start_period' does not match format '%Y-%m-%d %H:%M'
I precise start_period
is get by this : a = request.POST.get('start_period')
Upvotes: 0
Views: 1121
Reputation: 430
there are multiple ways to convert string to datetime in python. it is based on the requirement of the user which function to use.
one possible solution could be as given below.
import timestring
start_period = "2019-04-22 00:00"
print(timestring.Date(start_period))
Upvotes: 0
Reputation: 20490
May I suggest using dateutil module, where you do not need to provide the format string
from dateutil import parser
print(parser.parse("2019-04-22 00:00"))
#2019-04-22 00:00:00
Upvotes: 0
Reputation: 446
Like this it should work:
import datetime
start_period = "2019-04-22 00:00"
a = datetime.datetime.strptime(start_period, '%Y-%m-%d %H:%M')
Result:
datetime.datetime(2019, 4, 22, 0, 0)
Upvotes: 1