[Newbie] How to make the difference beetween 2 dates... ?

Mikael Olofsson mikael at isy.liu.se
Tue Nov 14 08:42:47 EST 2000


On 14-Nov-00 Christophe Prevost wrote:
 >  I have a month and a year and i want to know if this date is 2 months old
 >  from now
 >  In PHP it's quite simple : $TwoMonthAgo = MkTime(0, 0, 0, $Month-2, $Day,
 >  $Year);

The same principle seems to work very well i python:

>>> import time
>>> time.time()
974208614.846
>>> time.localtime(time.time())
(2000, 11, 14, 14, 30, 28, 1, 319, 0)
>>> time.localtime(time.mktime((2000, 11, 14, 0,0,0,0,0,0)))
(2000, 11, 14, 0, 0, 0, 1, 319, 0)
>>> time.localtime(time.mktime((2000, -1, 14, 0,0,0,0,0,0)))
(1999, 11, 14, 0, 0, 0, 6, 318, 0)
>>> time.localtime(time.mktime((2000, 0, 14, 0,0,0,0,0,0)))
(1999, 12, 14, 0, 0, 0, 1, 348, 0)

All you have to do is extract the current date from 

  time.localtime(time.time()),

subtract 2 from the month and stuff it back using 

  time.mktime(your_new_date)

and compare.

Good luck.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    14-Nov-00
Time:    14:33:20

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------



More information about the Python-list mailing list