Python Cookbook Third edition study Note five: datetime

Source: Internet
Author: User
Tags timedelta

The module representing time in Python is datetime, which introduces the following module
Datetime,timedelta
Datetime.today () #打印出当前的时间
E:\python2.7.11\python.exe e:/py_prj/python_cookbook.py
2017-04-26 21:58:05.663000
We can also add and subtract time. We're going to use the Timedelta module here.
This module has 5 important parameters, namely days,minutes,seconds,hours,weeks,microseconds 
Timedelta (DAYS=XX,MINUTES=XX,SECONDS=XX,HOURS=XX,WEEKS=XX,MICROSECONDS=XX)
Therefore, the time can be added and reduced based on these parameters.
Datetime.today ()
Datetime.today () +timedelta (days=1,minutes=1,seconds=1, hours=1,microseconds=1)
Get the following results, you can see the number of days, minutes, seconds, hours, microseconds on the basis of the previous add 1. If you want to use Timedelta (Weeks=1) in weeks


Timedelta maximum can only be added to the week, if you want to date the month and year of the addition and subtraction. How to operate it. This is going to use the following module
Relativedelta
Now=datetime.today ()
Now
Now+relativedelta (years=1,months=1)
You can see that the year and month have been added to the 1 operation

Relativedelta can operate on any unit of time. As follows
Now+relativedelta (years=1,months=1,hours=1,minutes =1,seconds=1,microseconds=1)    

If we want to arbitrarily get the difference of any two time period, how to do it. Like how much time we want to get between 2016-7-1 and 2016-8-5 .
You first need to format the date: The following represents 2016-07-01  :20 and 2016-08-05  15:10  
T1=datetime (7,1, +)
T2=datetime (8,5,ten)
time comparison with Relativedelta
D=relativedelta (T2,T1)
D
The following results are obtained. You can see that a Relativedelta object is returned, which includes the time difference. This sequence of operations is T2-T1


Returns the object is not too intuitive, how to get a specific time gap:
D.months,d.days,d.minutes
Get 1 4.
Introducing the usage of so many time modules, let's write an implementation: for example, today is Wednesday, I want to get the last Monday date is how much.
The implementation is as follows:
Get_previous_day (Dayname):
#首先创建出一个列表, which contains Monday to Sunday weekdays=[' Monday ',' Tuesday ',' Wednesday ', ' Thursday ' ,' Friday ',' Saturday ',' Sunday ']
Start_date=datetime.today ()
#weekday的作用是得出当日在这周中的索引. For example, the Monday to Sunday indexes were
Day_num=start_date.weekday ()
#得到目标日期的索引
Day_num_target=weekdays.index (Dayname)
#求得日期的差距, if the gap is 0, then days_ago=7, that's exactly one weeks apart.
days_ago= (7+day_num-day_num_target)%7
0:
days_ago=7
Target_date=start_date-timedelta (days=days_ago)
Target_date
__name__==' __main__ ':
#找到上一个周一的时间
Get_previous_day (' Monday ')
Get the following results.

In fact, we have an easier way. First quote Dateutil.rrule
Among them, Mo,tu,we,th,fr,sa,su represents Monday to Sunday respectively. is actually the first 2 capital letters in English
*
Now=datetime.today ()
Now
#下一个周一
Now+relativedelta (weekday=mo)
#下下的周一
Now+relativedelta (weekday=mo (+2))
#上一个周一
Now+relativedelta (weekday=mo (-1))
#上上个周一
Now+relativedelta (weekday=mo (-2))
The results are as follows:

Another calendar module is described below. Calendar
Let's look at the following usage:
Year=datetime.today (). Year
Month=datetime.today (). Month
Calendar.month (Year,month)
Print out a calendar for a year, a month

Print out a calendar for a year:
Calendar.calendar (year)

Determine if it is a leap year:
Calendar.isleap (year)
Determine how many days are in one months
Calendar.monthrange (Year,month)
Return results (5,30)
Where 5 represents the index of the day within the week. Equivalent to weekday. 30 Indicates how many days this month  

Python Cookbook Third edition study Note five: datetime

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.