Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to find out a date/time difference

Reply
Thread Tools

Re: How to find out a date/time difference

 
 
Nico Grubert
Guest
Posts: n/a
 
      05-24-2006


> I use datetime class in my program and now
> I have two fields that have the datetime format like this
> datetime.datetime(2006, 5, 24, 16, 1, 26)


> How can I find out the date/time difference ( in days) of such two
> fields?


Hi Lad,

you could do this:

>>> a = datetime.datetime(2006, 5, 24, 16, 1, 26)
>>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
>>> a-b

datetime.timedelta(4)
# 4 days

>>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
>>> x = a-b
>>> x

datetime.timedelta(4, 14400)
>>> str(x)

'4 days, 4:00:00'


Regards,
Nico
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      05-24-2006
And if you want the number of days:

py> d = datetime.datetime(2006,5,24,16,34) -
datetime.datetime(2006,5,23,12,1)
py> d.days
1
py> d = datetime.datetime(2006,5,24,16,34) -
datetime.datetime(2006,5,23,19,1)
py> d.days
0

 
Reply With Quote
 
 
 
 
Klaus Alexander Seistrup
Guest
Posts: n/a
 
      05-24-2006
Nico Grubert skrev:

> you could do this:
>
> >>> a = datetime.datetime(2006, 5, 24, 16, 1, 26)
> >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
> >>> a-b

> datetime.timedelta(4)
> # 4 days


Or

#v+

>>> print (a-b).days

4
>>>


#v-

Mvh,

--
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
How to find out a date/time difference Lad Python 2 05-24-2006 02:23 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments