If you are not familiar with the actual application solution of the Pyhon DateTime module, you do not know the Application Form of the specific example of the Pyhon DateTime module, the following articles are related to the typical examples, and I hope you will gain some benefits.
Common examples of the Pyhon DateTime Module
- <!--[if !supportLists]-->1、 <!--[endif]-->
Calculate the time difference between two specified time values
- Demo1:
- import datetime
- d1 = datetime.datetime(2005,12,31)
- d2 = datetime.datetime(2004,12,31)
- print (d1 - d2).days
Output:
- 365
Demo2: returns the execution time of a program.
- import datetime,time
- begin = datetime.datetime.now()
- time.sleep(5)
- begins = datetime.datetime.now()
- print "time is ",(begins - begin).seconds
Output: 5
Demo3: returns another time point of a time difference.
- import datetime,time
- begin = datetime.datetime.now()
- print begin
- d3= begin + datetime.timedelta(hours = 2)
- print d3
Output: Demo4, a time value separated by two hours: time and character type conversion
- import datetime,time
- dt_obj = datetime.datetime(2008, 11, 10, 17, 53, 59)
- date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
- print date_str
Output:
- 2008-11-10 17:53:59
PS: It can be formatted and output for a period of time! The above article describes common examples of the Pyhon DateTime module.