amit
amit

Reputation: 10261

Manipulating time in python

In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.

For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.

Also it should update the date var as well. For example, if the date is 24 Feb and time is 15:00 hrs and the delay time is 10 hrs, the jcarddeliver date should change to 25 Feb.

jcarddate = time.strftime("%a %m/%d/%y", time.localtime())
jcardtime = time.strftime("%H:%M:%S", time.localtime())
delay = raw_input("enter the delay: ")
jcarddeliver = ??

I just hope I am making sense.

Upvotes: 1

Views: 4833

Answers (4)

nabice
nabice

Reputation: 11

Try this:

now = time.time()
then = now + 365*24*3600
print time.strftime('%Y-%m-%d', time.localtime(then))

Upvotes: 0

Brian Hawkins
Brian Hawkins

Reputation: 2930

You could try the datetime module, e.g.

import datetime
now = datetime.datetime.now()
delay = float (raw_input ("enter delay (s): "))
dt = datetime.timedelta (seconds=delay)
then = now + dt
print now
print then

Upvotes: 3

D.Shawley
D.Shawley

Reputation: 59553

The result of time.time() is a floating point value of the number of seconds since the Epoch. You can add seconds to this value and use time.localtime(), time.ctime() and other functions to get the result in various forms:

>>> now = time.time()
>>> time.ctime(now)
'Fri Sep 04 16:19:59 2009' # <-- this is local time
>>> then = now + (10.0 * 60.0 * 60.0) # <-- 10 hours in seconds
>>> time.ctime(then)
'Sat Sep 05 02:19:59 2009'

Upvotes: 1

S.Lott
S.Lott

Reputation: 391818

" i need to set the jcarddeliver var to update itself with the value of the current date/time + delay"

How about reformulating this to

jcarddeliver should be the current date-time plus the delay.

The "update itself" isn't perfectly sensible.

Try the following:

  1. Try the most obvious way of computing "current date-time plus the delay"

  2. print the result.

  3. Try using localtime() on this result. What do you get?

Upvotes: 0

Related Questions