2

I have a string that represents the timedelta in the format X days HH:MM:SS.

Eg :

0 days 00:10:07
1 days 00:01:02

I want to convert it to

  1. python datetime.timedelta format.
  2. Minutes (i.e in above example it would say 10 mins and second example 1 day*60*24+1 hour *60 = 1500)

Can I use strptime method to parse it and convert to correct format? If not that, is there a way without using reg-ex re module ?

Edit :

  • A similar question on stack-overflow explains the usage of strptime method. However it assumes that the input is a specific datetime.

  • In my case, the interval is given as a timedelta but in string
    format.

  • The top voted answer uses the regex - I prefer to use the inbuilt datetime library to parse unless it cannot be done without regex

Spandan Brahmbhatt
  • 3,774
  • 6
  • 24
  • 36
  • the answer on this post answers it nicely. http://stackoverflow.com/questions/4628122/how-to-construct-a-timedelta-object-from-a-simple-string – Nick Brady Mar 10 '17 at 01:02
  • @NickBrady I hope the edit is able to to explain why this is not a duplicate question. – Spandan Brahmbhatt Mar 10 '17 at 01:22
  • Hi @Spandan, I'm sorry for not reading the question thoroughly enough.. I must have missed that. Looking at the docs https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior it seems to say pretty explicitly that the `.strptime` method isn't available for timedelta objects, though I don't personally know if there is an alternative method. Given the other answer is the highest voted it is _likely_ the best method for doing what you want, but perhaps not. I'm not leaving this as an answer in the hopes someone better helps you, or you post the answer you find yourself :). – Nick Brady Mar 10 '17 at 16:23

0 Answers0