Python practical date and time processing method summary, python Processing Method
The conversion principle is datetime-centered, starting point or intermediate. It is converted to the target object and covers the date conversion processing required in most business scenarios.
Steps:
1. Master several objects and their relationships
2. Understand the basic operation methods of each type of object
3. Conversion through conversion relationships
Objects involved
1. datetime
Copy codeThe Code is as follows:
>>> Import datetime
>>> Now = datetime. datetime. now ()
>>> Now
Datetime. datetime (2015, 1, 12, 23, 9, 12,946)
>>> Type (now)
<Type 'datetime. datetime'>
2. timestamp
Copy codeThe Code is as follows:
>>> Import time
>>> Time. time ()
1421075455.568243
3. time tuple
Copy codeThe Code is as follows:
>>> Import time
>>> Time. localtime ()
Time. struct_time (maid = 2015, tm_mon = 1, tm_mday = 12, tm_hour = 23, tm_min = 10, tm_sec = 30, tm_wday = 0, tm_yday = 12, tm_isdst = 0)
4. string
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M: % S ")
'2017-01-12 23:13:08'
5. date
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). date ()
Datetime. date (2015, 1, 12)
Datetime basic operations
1. Get the current datetime
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now ()
Datetime. datetime (2015, 1, 12, 23, 26, 24,475 680)
2. Get the date of the current day
Copy codeThe Code is as follows:
>>> Datetime. date. today ()
Datetime. date (2015, 1, 12)
3. Get tomorrow/N days before
Tomorrow
Copy codeThe Code is as follows:
>>> Datetime. date. today () + datetime. timedelta (days = 1)
Datetime. date (2015, 1, 13)
Three days ago
Copy codeThe Code is as follows:
>>> Datetime. datetime. now ()
Datetime. datetime (2015, 1, 12, 23, 38, 55,492 226)
>>> Datetime. datetime. now ()-datetime. timedelta (days = 3)
Datetime. datetime (2015, 1, 9, 23, 38, 57,593 63)
4. Obtain the start time and End Time of the current day (00:00:00)
Copy codeThe Code is as follows:
>>> Datetime. datetime. combine (datetime. date. today (), datetime. time. min)
Datetime. datetime (2015, 1, 12, 0, 0)
>>> Datetime. datetime. combine (datetime. date. today (), datetime. time. max)
Datetime. datetime (2015, 1, 12, 23, 59, 59,999 999)
5. Obtain the time difference between two datetime values.
Copy codeThe Code is as follows:
>>> (Datetime. datetime (,)-datetime. datetime. now (). total_seconds ()
44747.768075
6. Obtain the last day of the week/month
This week
Copy codeThe Code is as follows:
>>> Today = datetime. date. today ()
>>> Today
Datetime. date (2015, 1, 12)
>>> Sunday = today + datetime. timedelta (6-today. weekday ())
>>> Sunday
Datetime. date (2015, 1, 18)
This month
Copy codeThe Code is as follows:
>>> Import calendar
>>> Today = datetime. date. today ()
>>>_, Last_day_num = calendar. monthrange (today. year, today. month)
>>> Last_day = datetime. date (today. year, today. month, last_day_num)
>>> Last_day
Datetime. date (2015, 1, 31)
Obtain the last day of the previous month (possibly across years)
Copy codeThe Code is as follows:
>>> Import datetime
>>> Today = datetime. date. today ()
>>> First = datetime. date (day = 1, month = today. month, year = today. year)
>>> LastMonth = first-datetime. timedelta (days = 1)
Link Conversion
Conversion between several relationships
Datetime Object/String/timestamp/time tuple
Link conversion example
Datetime <=> string
Datetime-> string
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M: % S ")
'2017-01-12 23:13:08'
String-> datetime
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. strptime ("2014-12-31 18:20:10", "% Y-% m-% d % H: % M: % S ")
Datetime. datetime (2014, 12, 31, 18, 20, 10)
Datetime <=> timetuple
Datetime-> timetuple
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). timetuple ()
Time. struct_time (maid = 2015, tm_mon = 1, tm_mday = 12, tm_hour = 23, tm_min = 17, tm_sec = 59, tm_wday = 0, tm_yday = 12, tm_isdst =-1)
Timetuple-> datetime
Copy codeThe Code is as follows:
Timetuple => timestamp => datetime [see the following datetime <=> timestamp]
Datetime <=> date
Datetime-> date
Copy codeThe Code is as follows:
>>> Import datetime
>>> Datetime. datetime. now (). date ()
Datetime. date (2015, 1, 12)
Date-> datetime
Copy codeThe Code is as follows:
>>> Datetime. date. today ()
Datetime. date (2015, 1, 12)
>>> Today = datetime. date. today ()
>>> Datetime. datetime. combine (today, datetime. time ())
Datetime. datetime (2015, 1, 12, 0, 0)
>>> Datetime. datetime. combine (today, datetime. time. min)
Datetime. datetime (2015, 1, 12, 0, 0)
Datetime <=> timestamp
Datetime-> timestamp
Copy codeThe Code is as follows:
>>> Now = datetime. datetime. now ()
>>> Timestamp = time. mktime (now. timetuple ())
>>> Timestamp
1421077403.0
Timestamp-> datetime
Copy codeThe Code is as follows:
>>> Datetime. datetime. fromtimestamp (1421077403.0)
Datetime. datetime (2015, 1, 12, 23, 43, 23)