Time module and datetime module, datetime Module

Source: Internet
Author: User

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

Related Article

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.