0

I have a string called startTime and I want to add a duration (that's in minutes) to get a finishTime.

Here's some sample data:

startTime : 10:15

duration: 90

In python, what's the best way to add that duration so that the finishTime would be 11:45?

Thank so much, any help much appreciate.

Torxed
  • 22,866
  • 14
  • 82
  • 131
robster
  • 626
  • 1
  • 7
  • 22
  • Possible duplicate of [Datetime strptime in python](https://stackoverflow.com/questions/44596077/datetime-strptime-in-python) – Torxed Jan 30 '19 at 11:01
  • 2
    Possible duplicate of [What is the standard way to add N seconds to datetime.time in Python?](https://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python) – RvdK Jan 30 '19 at 11:02

3 Answers3

4

Try this:

import datetime

d = datetime.datetime.strptime("10:12","%H:%m") + datetime.timedelta(minutes=90)
new = d.strftime('%H:%m')

new will be 11:45.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
  • 1
    perfect thank you! I had a play and the %m should be %M but it fully works and I've since used that for some other conversions today as well. So know i know HOW it works also. Thanks again mister (and to everyone who helped) – robster Jan 31 '19 at 03:47
3

I recommend using pandas for tasks like this, since it is much more intuitive to use than using the datetime module directly.

import pandas as pd
startTime = '10:15'
duration = '90'
finishTime = (pd.to_datetime(startTime , format='%H:%M') 
              + pd.to_timedelta(duration + 'min')).strftime(format='%H:%M')

As pointed out by Torxed and shmee, pandas is not a built-in module and it is fairly large with around 70MB, thus it may not be suitable for everyone.
But since pandas is imho the best tool to process time series data and since the question looked like something which is commonly needed when processing time series, I thought a solution with pandas might be interesting/of use.

JE_Muc
  • 5,403
  • 2
  • 26
  • 41
  • @Torxed: That's quite harsh. You are right that pandas is a big module, but imho for working with data, pandas should be imported all the time anyways. Since for todays computer 66MB is not really that much, I only agree with you, if some standalone is to be created. Furthermore pandas is really easy to use and robust to errors, whereas datetime is much more complicated and prone to producing errors when you don't know exactly what you are doing. – JE_Muc Jan 30 '19 at 11:13
  • Since someone so kindly removed my comment, I can't really elaborate on what was harsh. I don't see why pointing out using a external library with a bunch of excess code to solve a task that can be solved with built in libraries - is harsh. But then again, suggesting things (while still saying this solves the problem) is apparently bad manners. Truth and opinions can hurt, i get that. But the line for what you can say gets tighter every day. So I guess I apologies. I just think users should be aware what they're getting when they get recommended a external lib. Helpers can hurt new developers. – Torxed Jan 30 '19 at 11:19
  • @Torxed I did not remove your comment. It was not **what you said**, but **the way you said it**. Just pointing it out in a friendly way without words like *"odd"* or *"shit ton"* would have been better. I agree with you that pandas might be overkill for this. But it is most likely that he will do more than just find a `finishTime`. I guess he'll want to use the finishTime for something. And if it is for some timeseries stuff, pandas will be the right tool. – JE_Muc Jan 30 '19 at 11:28
  • Under some circumstances, e.g. in corporate environments, you might not have access to 3rd party packages. Therefore, I prefer solutions using built ins as well. – shmee Jan 30 '19 at 11:28
  • @shmee I fully agree. Still I think that it is always good to post *different possibilites to solve a problem*, so that the one asking the question may select the one suiting his needs the best. – JE_Muc Jan 30 '19 at 11:30
  • 2
    Not at all debating that :) However, I feel that a recommendation to use a 3rd party lib should come along with some disclaimer stating that fact and should ideally contain a link to the package homepage/documentation if available. On an aside: I think you should add `import pandas as pd` for completeness sake :D – shmee Jan 30 '19 at 11:40
  • 1
    @shmee Me neither and thanks for the hint. :) I implemented both and added a short disclaimer. – JE_Muc Jan 30 '19 at 11:47
  • I would like to point out that you make good and valid observations Scotty. And I appreciate you being a reasonable person to communicate. Using words like "shit ton" or "odd" is in my every day life not deemed as harsh. Depending on the context of course. Again, apologies for any harsh words - just hope you reading it realize that meaning behind words is as different for the writer as I understand it is for the reader. And that we call can agree that - as long as we're open to re-writing it to meet a common ground - it should all be fine. Upvoted for a clear description and good reflections. – Torxed Jan 30 '19 at 11:48
  • 1
    @Torxed Thanks and not a problem at all. Also no upvote needed, if my solution is not what you'd support. I fully understand that pandas may not be the right tool for a simple case like this. Also thanks for pointing that out. I don't mind harsh words, but especially on the internet when you don't see the other person, a friendly comment may yield a better result. – JE_Muc Jan 30 '19 at 12:02
  • 2
    Although I cannot speak for Torxed, _I_ upvoted your answer because you have made it a complete and helpful one. It might not be the approach I'd take, but that does not make it less worthy of an upvote, imho. – shmee Jan 30 '19 at 12:10
  • 2
    I concur with @shmee. Just because I wouldn't use or don't like something, doesn't make it less viable/good. You have to set aside your own personal believes and look at things like these from a perspective of neutral and critical thinking. This is a good answer, no matter what I think or believe. It reflects some good points, It's helpful if you're willing to use external libraries - which you point out in your answer. That alone is worth an up-vote. Good manners should give extra credit, sadly there's no points for that - so you'll have to take my sentences for what they're worth. Good job:) – Torxed Jan 30 '19 at 12:22
  • 1
    Thanks then. :) And I learned something: Pointing out drawbacks of a specific solution is crucial. I'll keep that in mind for my next answers. – JE_Muc Jan 30 '19 at 12:29
1

You could use pd.to_datetime in order to convert the strings to datetime objects, and add the minutes using pd.to_timedelta. Finally convert the datetime to the desired format using strftime:

import pandas as pd
(pd.to_datetime( '10:15', format='%H:%M') + pd.to_timedelta(90, unit='m')).strftime('%H:%M')
#'11:45'
yatu
  • 86,083
  • 12
  • 84
  • 139
  • 1
    Why the downvote? reason please, perhaps I can improve the answer with your feedback – yatu Jan 30 '19 at 11:09
  • 1
    Perhaps you could give me some time to elaborate on my answer? – yatu Jan 30 '19 at 11:11
  • 1
    Again imho the comments from @Torxed are quite harsh. In my opinion, if someone tries to help, give him the appreciation. Even if the answer is not 100% perfect (especially in the first 15 seconds after posting it). But downvoting an answer right away is frustrating for someone posting an answer and trying to help... I thus upvoted your answer to compensate the downvote. ;) – JE_Muc Jan 30 '19 at 11:20
  • Sure, I do the same thing. I post a semi-complete answer every day. But I make sure the code provided can be executed as-is before posting. Otherwise, you're posting for the points and glory not to be helpful. I'll get hate for not being polite here, but the truth is that this answer isn't helpful - **as is at the moment**. You can't run this, there's no explanation as to where these functions are imported from. The answer was two words "use this:" and then code that can't be executed. What's more harsh, giving someone something they can't use or pointing out "this isn't useful"? – Torxed Jan 30 '19 at 11:21
  • Totally agree and appreciate your comment, returned :) – yatu Jan 30 '19 at 11:21
  • I even pointed out, I usually go back the questions and review my down votes when the questions are actually useful/helpful. And I also provided information as to why the down vote was given so that the writer can improve upon it. Never mind that the writer in question took time to ask why rather than keep improving his or her question. But again, I clearly stated why I gave the down vote, and I have no problem removing it when the answer is useful. Points are given to good answers, time has nothing to do with it. – Torxed Jan 30 '19 at 11:23
  • There are links to the exact libraries... I added the import, but IMO a link to the library is enough. Anyway I do not share at all what your view, specially given that I always make an effort in providing a good explanation in my answers, and reference all functions used. I've already exposed why people who make an effort in answering should be given some time – yatu Jan 30 '19 at 11:26
  • @Torxed Yeah, it is true that non-running code does not help that much. But did it never happen to you, that you posted a code snippet from the console and forgot one line or added a typo since you named the variables in a different way than in the question? This happens to me quite often, and some times it takes me some time to realize it. Just give the people some time and post a friendly hint. This will help alot more than downvoting without commenting. And thanks Yatu – JE_Muc Jan 30 '19 at 11:34
  • I should point out that both you and @Scotty1- are welcome to have an actual conversion about these things any time, any day if you feel like it. I doubt that comments are the place to be for these things. – Torxed Jan 30 '19 at 11:34
  • @Torxed I agree with the point that the comments are not for conversations. But looking at the length of the comments... Wasn't it you who started the conversation? :) – JE_Muc Jan 30 '19 at 11:38