Python uses the datetime module to calculate various time interval methods, pythondatetime
This example describes how to use the datetime module in python to calculate various time intervals. Share it with you for your reference. The specific analysis is as follows:
In python, The datetime module can be used to conveniently calculate the difference between two time points. The unit of datetime time difference can be days, hours, seconds, or even microseconds, the following code demonstrates the powerful functions of the datetime module in calculating the time difference.
#-*-Coding: UTF-8 -*-#! /Usr/bin/env pythonimport datetime # Calculate the normal time of datetime in d1 = datetime. datetime (2013, 8, 15, 50) d2 = datetime. datetime (2013, 8, 9, 0) d3 = datetime. timedelta (microseconds = 5000) print U' difference: % s microsecond '% (d1-d2 ). microsecondsprint U' difference: % s second '% (d1-d2 ). secondsprint U' difference: % s day '% (d1-d2 ). daysprint U' time interval: % s microsecond '% d3 # Time Zone conversion. The current system is located in the time zone + 1d = datetime. datetime. now () d = d + datetime. timedelta (seconds = 3600) print dprint d. ctime ()
The output result is as follows:
Difference: 0 microseconds difference: 67260 seconds difference: 0 days interval: 0: 00: 00.005000 microseconds 11:29:29. 663000Fri Aug 30 11:29:29 2013
I hope this article will help you with Python programming.