Analysis of Common examples of the Pyhon DateTime Module

Source: Internet
Author: User

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

 
 
  1. <!--[if !supportLists]-->1、 <!--[endif]--> 

Calculate the time difference between two specified time values

 
 
  1. Demo1:   
  2. import datetime  
  3. d1 = datetime.datetime(2005,12,31)  
  4. d2 = datetime.datetime(2004,12,31)  
  5. print (d1 - d2).days   

Output:

 
 
  1. 365 

Demo2: returns the execution time of a program.

 
 
  1. import datetime,time  
  2. begin = datetime.datetime.now()  
  3. time.sleep(5)  
  4. begins = datetime.datetime.now()  
  5. print "time is ",(begins - begin).seconds   

Output: 5

Demo3: returns another time point of a time difference.

 
 
  1. import datetime,time  
  2. begin = datetime.datetime.now()  
  3. print begin  
  4. d3= begin + datetime.timedelta(hours = 2)  
  5. print d3  

Output: Demo4, a time value separated by two hours: time and character type conversion

 
 
  1. import datetime,time  
  2. dt_obj = datetime.datetime(2008, 11, 10, 17, 53, 59)  
  3. date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")  
  4. print date_str   

Output:

 
 
  1. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.