Python time differences

后端 未结 4 2109
忘掉有多难
忘掉有多难 2020-12-17 09:07

I have two time objects.

Example

time.struct_time(tm_year=2010, tm_mon=9, tm_mday=24, tm_hour=19, tm_min=13, tm_sec=37, tm_wday=4, tm_yday=267, tm_is         


        
4条回答
  •  孤街浪徒
    2020-12-17 09:49

    There is another way to find the time difference between any two dates (no better than the previous solution, I guess):

     >>> import datetime
     >>> dt1 = datetime.datetime.strptime("10 Oct 10", "%d %b %y")
     >>> dt2 = datetime.datetime.strptime("15 Oct 10", "%d %b %y")
     >>> (dt2 - dt1).days
     5
     >>> (dt2 - dt1).seconds
     0
     >>>
    

    It will give the difference in days or seconds or combination of that. The type for (dt2 - dt1) is datetime.timedelta. Look in the library for further details.

提交回复
热议问题