David542
David542

Reputation: 110562

Add a minute to a string

Is there an easy way to add a minute to a TIMESTAMP string? For example:

t1 = '02:20:03'
t1 + 1min = '02:21:03'

The current way I'm doing it is

(datetime.combine(date.today(), self.video_length) - timedelta(minutes=2)).time()

But that seems a bit cumbersome. Is there a simpler way to do it?

Upvotes: 2

Views: 69

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114098

the dateutil library can parse most things you throw at it magically

(you may have to pip install python-dateutil)

import dateutil.parser as p

(p.parse("02:20:03")+datetime.timedelta(minutes=2)).time()

maybe?

a better idea would be to probably store it as a datetime and only print it to a string when you need to (but I dont have enough context to know if that is viable or not)

Upvotes: 5

Related Questions