Issue
The following ValueError
is being raised while running the following code. The date is passed as a string from an another component, where I need to strip out the time.
ValueError: time data '2022-03-24T14:02:24.6413975' does not match format '%Y-%m-%d %H:%M:%S,%f'
The code:
from datetime import datetime
date='2022-03-24T14:02:24.6413975'
time = datetime.strptime(date, "%Y-%m-%d %H:%M:%S,%f")
if time > '09:30' :
print("do some thing")
Solution
This should work:
import datetime
date='2022-03-24T14:02:24.6413975'
time = date.split('T')[1].split(':')
time = datetime.time(int(time[0]), int(time[1]), int(time[2].split('.')[0]), int(time[2].split('.')[1][:6]))
if time > datetime.time(9, 30) :
print("do some thing")
Output:
do some thing
This just takes date
, splits it T
, splits the second part of the resulting string at every :
, and passes all of them to datetime.time
. The last two arguments to datetime.time
have to be split a the decimal to get the microseconds, and the last one has to be shortened because of the limit on how long datetime
allows microseconds to be.
Answered By - catasaurus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.