Time processing is the most common requirement in our daily development, such as: getting the current datetime, getting today's date, getting tomorrow/previous N days, getting the start and end time of the day (00:00:00 23:59:59 ), get the time difference between two datetimes, get the last day of this week/this month/last month, etc. These conversions seem messy and difficult to remember, so today we will summarize Python’s time processing.
Principle: Take datetime as the center, starting point or transit, and convert it into the target object, covering 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. Convert through transformation relationships
datetime is date A combination with time, including all information about date and time.
The function prototype is:
datetime. datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
The meaning of each parameter is the same as that in the date and time constructors. Pay attention to the range of parameter values.
Example:
1. Get the datetime object
The code is as follows:
import datetime now = datetime.datetime.now() #输出:datetime.datetime(2016, 11, 26, 8, 34, 30, 876359)
2.Get the timestamp(timestamp)
Time The stamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also called Unix Timestamp.
The code is as follows:
import time time.time() #输出:1480120686.733905
3. Get time tuple (tuple)
The code is as follows:
import time time.localtime() #输出:time.struct_time(tm_year=2016, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=39, tm_sec=33, tm_wday=5, tm_yday=331, tm_isdst=0)
4. Get time string (string)
String format parameter list:
datetime. strftime (format)
%a: Abbreviation of week. For example, Wednesday is Web
%A: The full letter of the week. For example, Wednesday is Wednesday
%b: the abbreviation of the month. For example, April is Apr
%B: the full month. For example, the month of April is April
%c: string representation of date and time. (For example: 04/07/10 10:43:39)
%d: The number of days in this month (the number of days in this month)
%f: Microseconds (range [0,999999 ])
%H: Hour (24-hour format, [0, 23])
%I: Hour (12-hour format, [0, 11])
%j: The number of days in the year [001,366] (the day of the year)
%m: month ([01,12])
%M: minute ([00,59])
%p: AM or PM
%S: Seconds (range is [00, 61], why not [00, 59], refer to the python manual~_~)
%U: The number of weeks in the year and the week in the year), Sunday as The first day of the week
%w: The number of days today is in this week, the range is [0, 6], 6 means Sunday
%W: The number of weeks in the year (the first week of the year), Monday is the first day of the week
%x: date string (such as: 04/07/10)
%X: time string (such as: 10:43:39)
%y: Year represented by 2 digits
%Y: Year represented by 4 digits
%z: Interval with UTC time (if it is local time, return an empty string)
%Z: Time zone name (if is the local time, returns an empty string)
%%: %% => %
The code is as follows:
import datetime datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") '2016-11-26 08:40:39'
5.date (date)
Code As follows:
import datetime datetime.datetime.now().date() datetime.date(2016, 11, 26)
6. Get today’s date
The code is as follows:
import datetime datetime.date.today() datetime.date(2016, 11, 26)
7. Get tomorrow/previous N days
Tomorrow
The code is as follows:
import datetime datetime.date.today() + datetime.timedelta(days=1) datetime.date(2016, 11, 27)
Two days ago
The code is as follows:
import datetime >>> datetime.datetime.now() datetime.datetime(2016, 11, 26, 8, 42, 59, 665368) >>> datetime.datetime.now() - datetime.timedelta(days=3) datetime.datetime(2016, 11, 24, 8, 43, 14, 696948)
8. Get the start and end time of the day (00:00:00 23:59:59)
The code is as follows:
import datetime datetime.datetime.combine(datetime.date.today(), datetime.time.min) datetime.datetime(2016, 11, 26, 0, 0) datetime.datetime.combine(datetime.date.today(), datetime.time.max) datetime.datetime(2016, 11, 26, 23, 59, 59, 999999)
9. Get the time difference between two datetimes
The code is as follows:
import datetime (datetime.datetime(2016,12,13,12,0,0) - datetime.datetime.now()).total_seconds() 1480506.809658
10. Get the last day of this week/this month/last month
This week
The code is as follows:
import datetime today = datetime.date.today() #输出: datetime.date(2016, 11, 26) sunday = today + datetime.timedelta(6 - today.weekday()) #输出:datetime.date(2016, 11, 27)
This month
The 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) #输出:datetime.date(2016, 11, 30)
11. Get the last day of the previous month (may span New Years )
The 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) #输出:datetime.date(2016, 10, 31)
Other usage examples:
The code is as follows:
#当月1号 datetime.date(datetime.date.today().year,datetime.date.today().month,1) #当月1号 datetime.date.today().replace(day=1) #上月1号 (datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=1)
and above The above is a summary of Python time acquisition and conversion knowledge introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more Python time acquisition and conversion knowledge summary related articles, please pay attention to the PHP Chinese website!