When writing code, you will always encounter a variety of time processing and requirements. To summarize:
First of all, this article is a summary of the DateTime module.
Begin.
First, DateTime basic operation
1. Get the current datetime
1 times = datetime.datetime.now ()2 print time3# Output The following date-the hour (accurate to microseconds, 6 digits after decimal point)45 2018-01-11 20:19:34.794000
2. Get Date of day
1 time = datetime.date.today ()2 print time3# The output results are as follows: 45 2018-01-11
3. Get tomorrow/the first n days
Tomorrow
1 time = Datetime.date.today () +datetime.timedelta (Days=1)2print time 3 # the output is: 4 5 2018-01-12
3 days ago
1 time = Datetime.date.today ()-datetime.timedelta (days=3)2print time 3 # the output is: 4 5 2018-01-08
Second, the time of the data type conversion
1. DateTime type converted to String type
1 time = Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s')2 Print time3# output:45 2018-01-11 20:41:23
2. DateTime type is converted to date type (remember this notation, remove date is the exact time, plus date is the day)
1 time = Datetime.datetime.now (). Date ()2print time3# The output is:45 2018-01-11
Third, plus timedalta can be calculated in time (days)
Example:
1 time = Datetime.datetime.now (). Date ()-datetime.timedelta (days = 1) #这里如果不写days默认也是days2 Print time3# output:45 2018-01-10
End
The end version allows you to view the links below.
This digest from: http://www.wklken.me/posts/2015/03/03/python-base-datetime.html
Python-Foundation-time Date Processing summary (DateTime module)