Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Time

Status
Not open for further replies.

ahuacatlan

Programmer
Apr 22, 2006
5
US
Hi,
I would like to take a variable that is the current time and add times to it. I would like the script to print what 5:30 + 45 minutes is.

This is what I have so far:

from time import localtime, strftime

currenttime = strftime("%H:%M", localtime())
currentday = strftime("%a", localtime())
print "The current time is", currenttime, "and the today is", currentday

if currentday == "Mon":
print "lessons begin in", currenttime + 45 minutes

I need help with what to put in place of the 45 minutes
Thanks!
james
 
Please use [ignore]
Code:
 and
[/ignore] to type in code blocks. It makes reading it much nicer.

localtime() takes an argument, which is a time expressed in seconds. So you can just add 45 minutes in seconds to the current time to get the answer you want:

Code:
import time
currenttime = time.time()
print time.strftime('%H:%M', time.localtime(currenttime + 45 * 60))

-
 
Thanks!
but is there a way to display the time in a 12 hour format?
 
There sure is and you can find it in the python manual in section 6.11 "time -- Time access and conversions".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top