Difference Between Two datetimes

W. eWatson wolftracks at invalid.com
Mon Dec 28 01:42:48 EST 2009


You're right. Y. Works fine. The produces datetime.datetime(2009, 1, 2, 
13, 1, 15).
If I now use
t2=datetime.datetime.strptime("2009/01/04 13:01:15","%Y/%m/%d %H:%M:%S")
I get tw as
datetime.datetime(2009, 1, 4, 13, 1, 15)
Then t2-t1 gives,
datetime.timedelta(2)
which is a 2 day difference--I guess. Strange.
Changing
t2=datetime.datetime.strptime("2009/01/04 14:00:30","%Y/%m/%d %H:%M:%S")
and differencing gives me,
datetime.timedelta(2, 3555), which seems to indicate a 2 day and 3555 
second difference. Interesting, but I think there must be another way to 
do this. Maybe not.

Roy Smith wrote:
> In article <hh9dmv$f9s$1 at news.eternal-september.org>,
>  "W. eWatson" <wolftracks at invalid.com> wrote:
> 
>> t1=datetime.datetime.strptime("2009/01/02 13:01:15","%y/%m/%d %H:%M:%S")
>> doesn't do it.
>> ValueError: time data did not match format:  data=2009/01/02 13:01:15 
>> fmt=%y/%m/%d %H:%M:%S
> 
> The first thing that jumps out at me is that %y is the two-digit year.  You 
> want %Y for 4-digit year.
> 
> One thing to keep in mind is that "2009/01/02 13:01:15" is ambiguous 
> without a time zone.  Even if you assume that both timestamps were from the 
> same location, you need to know what daylight savings rules that location 
> uses, to do this right.



More information about the Python-list mailing list