1

I need to convert time format of '83 days, 17:02:10.401369' to %H:%M:%S:%f

res_time = '83 days, 17:02:10.401369'
                
res_formated = datetime.strptime(res_time, "%H:%M:%S.%f")

Value Error: time data '83 days, 17:02:10.401369' does not match format '%H:%M:%S.%f'

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Split the string on `,` and try with the time only. – Klaus D. May 03 '21 at 04:52
  • Hi @KlausD. thanks for your suggestion. what if I want to add the 83 days to the 17:02:10.401369? – Kenneth Varias May 03 '21 at 05:49
  • related: [How to construct a timedelta object from a simple string](https://stackoverflow.com/q/4628122/10197418). Note that `pandas.to_timedelta` can readily parse the string, see e.g. [here](https://stackoverflow.com/questions/44611642/turn-a-string-back-into-a-datetime-timedelta). – FObersteiner May 03 '21 at 06:02

1 Answers1

0

like @Klaus D. say, use split to use the string:

res_time = '83 days, 17:02:10.401369'

res_time = res_time.split(",")[1]

res_time = res_time.strip() // Delete the white space before and after the string

res_formated = datetime.strptime(res_time, "%H:%M:%S.%f")
Max
  • 836
  • 6
  • 13