Python module knowledge 2: Time and date Calendar module Time, Datetime, Calendar, pythondatetime
1. time Module
Why did the time start in 1970? Because Linux was used in that year, the time is usually represented by the following methods:
Timestamp: the second after January 1, January 1, 1970, that is, time. time () can get the current timestamp.
Formatted string:, for example: time. strftime ('% Y-% m-% D'). For details, see the case.
Structured time, in the form of tuples, contains nine elements: year, day, week, etc... for example, time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 27, tm_hour = 15, tm_min = 16, tm_sec = 53, tm_wday = 5, tm_yday = 147, tm_isdst = 0 );
Common usage:
Time. time () returns the timestamp of the current time.
Time. ctime () returns the string format of the time, for example, Sat Dec 30 16:52:39 2017.
Time. gmtime () returns the structured time format, which is Greenwich Mean time.
Time. localtime () local time, such as Chinese time and Greenwich Mean time, is in structured format,
Time. mktime () converts a structured time to a timestamp.
Time. sleep () time pause
Time. strftime () converts structured time into formatted characters
Time. strptime () parses a time string into a time tuple
Case 1: time. time returns the timestamp of the current time
Case 2: string format of time. ctime returned time
Case 3: time. gmtime () returns the structured time format, which is Greenwich Mean time, which is different from the local time.
Case 4: Structured time application case. You can obtain each element and set it to a different format.
Case 5: time. localtime () local time, in the structured format, is different from Greenwich Mean time, Difference time Zone
Case 6: time. mktime () converts the structured time into a timestamp
Case 7: time. sleep () pause
Case 8: time. strftime () converts structured time into formatted characters
Strftime requires two parameters. The first is the format, which is case sensitive, and the second is the structured time. If t is not specified, time. localtime () is passed in (). If any element in the tuples crosses the border, the ValueError will be thrown.
Time and date formatting symbols in python:
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)
% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
% J one day in the year (001-366)
% P local equivalent of A. M. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Z Current Time Zone name
% Itself
Case 9: time. strptime converts other formats to structured formats
2. datetime Module
Common modules:
Datetime. date. today () Get the current date
Datetime. date. fromtimestamp () converts the timestamp to the date format
Datetime. datetime. now () returns the current date and time, in the format of 11:19:59. 128461
Convert timetuple () to struct_time
Replace () replaces some data, replae (yyyy, mm, dd)
Case 10: datetime. date. today () Get the current date
Case 11: Convert timestamp into Date Format
Case 12: datetime. datetime. now () returns the current date and time
Case 13: Convert timetuple () to struct_time
Case 14: replace () replaces some data, replae (yyyy, mm, dd)
Case 15: strptime () string formatting
Case 16: Time addition and subtraction
3. CalendarModule
For example, print a month's character calendar.
Monday is the default first day of every week, and Sunday is the default last day. To change the settings, call the calendar. setfirstweekday () function.
Common attributes:
Calendar. calendar (year, w = 2, l = 1, c = 6 ). Print the calendar.
Calendar. firstweekday () returns the setting of the start date of the current week. By default, 0 is returned when the caendar module is loaded for the first time, that is, Monday.
Calendar. isleap (year) to determine whether the year is a leap year
Calendar. leapdays (y1, y2) returns the total number of leap years between Y1 and Y2.
Calendar. monthcalendar (year, month) returns the list of single-layer nesting. Each sublist is a week number. The date except month of Year is set to 0, and the days in the range are represented by the day of the month, starting from 1.
Calendar. monthrange (year, month) returns two integers. The first number is the date code of the first date of the month, Monday is 0, from the second z to the day of the month.
Calendar. setfirstweekday (weekday) sets the start date of each week. 0 (Monday) to 6 (Sunday ).
Opposite to calendar. timegm (tupletime) and time. gmtime: returns the timestamp of the time in the form of a time tuple.
Calendar. weekday (year, month, day) returns the date code of the given date. 0 (Monday) to 6 (Sunday ). The month is from 1 (January 1, January) to 12 (January 12, December ).
Case 17: Print calendar:
Returns a multi-row response with a 3-month row separated by c. The daily width interval is w characters. The length of each line is 21 * W + 18 + 2 * C. L indicates the number of rows per week.
Case 18: calendar. firstweekday (). The default start position is returned.
Case 19: calendar. isleap (year), used to determine whether the year is a leap year
Case 20: calendar. leapdays (y1, y2) returns the total number of leap years between Y1 and Y2.
Case 21: print the calendar of the current month. month (year, month, w = 2, l = 1)
Case 22: calendar. monthcalendar (year, month) returns a single-layer nested list of integers.
Case 23: calendar. monthrange (year, month) returns two integers. The first number is the date code of the first date of the month, and the second is the date number of the current month.
Case link: https://pan.baidu.com/s/1o7JgBwu password: mf8r
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.