Time module and datetime module, datetime Module
Time module and datetime Module 1. Call
Import time # Call time module
Ii. usage method 1. time. time
Obtain the timestamp. The number of seconds from January 1, 1970 of the year when Linux was born to the time when the program was executed.
2. time. sleep ()
Pause the program for several seconds.
Time. sleep (3) # Pause the program for 3 seconds
3. time. clock ()
Calculate the time required by the CPU to execute the program except the time used by time. sleep.
Import time
Time. sleep (3)
Print (time. clock () # Calculate the time consumed by the CPU to execute the "import time" and "print" statements.
4. time. gmtime ()
Structured output
Import time
Print (time. gmtime () # time. struct_time (tm_year = 2017, tm_mon = 10, tm_mday = 26, tm_hour = 3, tm_min = 8, tm_sec = 57, tm_wday = 3, tm_yday = 299, tm_isdst = 0)
5. time. localtime ()
Structured output
import time
print(time.localtime()) # time.struct_time(tm_year=2017, tm_mon=10, tm_mday=26, tm_hour=11, tm_min=12, tm_sec=8, tm_wday=3, tm_yday=299, tm_isdst=0)
6. time. strftime ()
Custom time format output
% Y year.
% M month.
Number of % d.
The hour in the 24-hour format of % H.
% M minutes.
% S seconds.
% Z Time zone offset from UTC.
% A abbreviation of the local time week.
% A full name of the local time week.
% B abbreviation of the local time month.
% B full name of the local time month.
% C week refers to the hour, minute, and second year in the 24-hour format of the month.
% I 12 hour.
% P is in AM or PM.
Import time
C = time. localtime ()
Print (time. strftime ('% y-% m-% d % H: % M: % s', c) #17-10-26 19:57:28
7. time. strptime
Convert custom format time to structured time
A = time. strptime ('2017-09-08 18:48:35 ',' % Y-% m-% d % H: % M: % s ')
Print (a) # time. struct_time (tm_year = 2017, tm_mon = 9, tm_mday = 8, tm_hour = 18, tm_min = 48, tm_sec = 35, tm_wday = 4, tm_yday = 251, tm_isdst =-1)
Print (a. tm_mday) #8, the day of November
Print (a. tm_wday) #4, the day of the week
Print (a. tm_year) #2017, year
8. time. ctime
Import time
Print (time. ctime () # Fri Oct 27 09:38:42 2017
9. time. mktime
Converts a structured time to a timestamp.
Import time
Print (time. mktime (time. localtime () #1509068449.0
Iii. datetime Module
Import datetime
Print (datetime. datetime. now () #09:47:23. 406146