How Compute # of Days between Two Dates?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How Compute # of Days between Two Dates?

Wayne Watson
That's the question in Subject. For example, the difference between 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in years between, say, 1990 and 2050. In other words not some really strange period of time well outside our current era of history.
--
Signature.html
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
            
          Interesting government experience prior to their presidency
          or bid for it:
             Abraham Lincoln: 2 years; George Washington: 0 years;
              Dwight Eisenhower: 0 years; James Buchanan: 29 years*
                 Barack Obama: 11 years; John McCain: 26 years

              * Not a particularly good president at all
            
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: How Compute # of Days between Two Dates?

William Allison-2
Wayne Watson wrote:
> That's the question in Subject. For example, the difference between 08/29/2008
> and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4,
> leap year--extra day in Feb. I'm really only interested in years between, say,
> 1990 and 2050. In other words not some really strange period of time well
> outside our current era of history.

I've used the datetime module to do something similar.

Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> today = datetime.date.today()
>>> print today
2008-09-01
>>> last_year = datetime.date(2007, 9, 1)
>>> print today - last_year
366 days, 0:00:00
>>>

HTH,
Will

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: How Compute # of Days between Two Dates?

greg whittier
In reply to this post by Wayne Watson
On Mon, Sep 1, 2008 at 10:17 AM, Wayne Watson
<[hidden email]> wrote:
> That's the question in Subject. For example, the difference between
> 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
> 03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
> years between, say, 1990 and 2050. In other words not some really strange
> period of time well outside our current era of history.

You want the datetime module.

>>> from datetime import datetime
>>> datetime(2008,03,03) - datetime(2008,2,28)
datetime.timedelta(4)
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: How Compute # of Days between Two Dates?

Wayne Watson
How do I just get the current time? The following gives me 00:00:00.

import datetime

dt = datetime.time()
print "dt: ", dt

greg whittier wrote:
On Mon, Sep 1, 2008 at 10:17 AM, Wayne Watson
[hidden email] wrote:
  
That's the question in Subject. For example, the difference between
08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and
03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in
years between, say, 1990 and 2050. In other words not some really strange
period of time well outside our current era of history.
    

You want the datetime module.

  
from datetime import datetime
datetime(2008,03,03) - datetime(2008,2,28)
        
datetime.timedelta(4)

  

--
Signature.html
           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
            
          Interesting government experience prior to their presidency
          or bid for it:
             Abraham Lincoln: 2 years; George Washington: 0 years;
              Dwight Eisenhower: 0 years; James Buchanan: 29 years*
                 Barack Obama: 11 years; John McCain: 26 years

              * Not a particularly good president at all
            
                    Web Page: <www.speckledwithstars.net/>

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: How Compute # of Days between Two Dates?

John Fouhy
2008/9/2 Wayne Watson <[hidden email]>:
> How do I just get the current time? The following gives me 00:00:00.
>
> import datetime
>
> dt = datetime.time()

datetime.datetime.today() will give you "now" as a datetime.  You can
then extract a time object from it:

>>> datetime.datetime.today()
datetime.datetime(2008, 9, 2, 13, 17, 13, 345793)
>>> datetime.datetime.today().time()
datetime.time(13, 17, 15, 786378)

--
John.
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor