python 时间操作

Adding Dates and Times in Python

Mon, Oct 19, 2009

Tech Tips

Using the built-in modules datetime and timedelta, you can perform date and time addition/subtraction in python:

  1. from datetime import datetime  
  2. from datetime import timedelta  
  3.   
  4. #Add 1 day  
  5. print datetime.now() + timedelta(days=1)  
  6.   
  7. #Subtract 60 seconds  
  8. print datetime.now() - timedelta(seconds=60)  
  9.   
  10. #Add 2 years  
  11. print datetime.now() + timedelta(days=730)  
  12.   
  13. #Other Parameters you can pass in to timedelta:  
  14. # days, seconds, microseconds,  
  15. # milliseconds, minutes, hours, weeks  
  16.   
  17. #Pass multiple parameters (1 day and 5 minutes)  
  18. print datetime.now() + timedelta(days=1,minutes=5)  

Here is a python reference that gives more examples and advanced features:
http://docs.python.org/library/datetime.html

原文地址:https://www.cnblogs.com/lexus/p/1832943.html