python - Delta time string to datetime object -
given string looks "hours:5 minutes:34 seconds:28"
or "minutes:34 seconds:28"
, there pythonic way convert datetime
object? not want use regex if there's easier way.
yes, there is. can this:
import time datetime_string = "hours:5 minutes:34 seconds:28" if "hours" in datetime_string: datetime_object = time.strptime(datetime_string, "hours:%h minutes:%m seconds:%s") elif "minutes" in datetime_string: datetime_object = time.strptime(datetime_string, "minutes:%m seconds:%s") else: datetime_object = time.strptime(datetime_string, "seconds:%s")
note: when create datetime object, values not provide filled default values.so, in case datetime_string
contains seconds, hours , minutes set 0
.
Comments
Post a Comment