The examples in this article describe how Python calculates various time intervals using a datetime module. Share to everyone for your reference. The specific analysis is as follows:
Python through the datetime module can be very convenient to calculate the difference of two times, datetime units can be days, hours, seconds, or even microseconds, the following code shows the DateTime module in the calculation of the power of time lag
#-*-Coding:utf-8-*-
#!/usr/bin/env python
import datetime
#datetime一般的时间计算
d1 = datetime.datetime ( 2013, 8, 05,15,50)
D2 = Datetime.datetime (2013, 8, 4,21,9,0,0)
d3 = Datetime.timedelta (microseconds=5000
) Print u ' difference:%s microseconds '% (D1-D2). microseconds
print u ' difference:%s seconds '% (d1-d2). Seconds
Print u ' difference:%s days '% (D1-D2).
Day Print U ' interval:%s microsecond '%d3
#时区转换, current system in time zone +1
d = datetime.datetime.now ()
d = d + Datetime.timedelta (seconds= 3600)
Print D
print d.ctime ()
The output results are as follows:
Difference: 0 microseconds
difference: 67,260 seconds
difference: 0 days
interval: 0:00:00.005000 microsecond
2013-08-30 11:29:29.663000
Fri Aug 30 11:29:29 2013
I hope this article will help you with your Python programming.