Python--time (Time) module

Source: Internet
Author: User

To use a module, first import the module in

Import  Time

Let's start by importing the module we need to use for this article.

Let's start with the time module, the function in the time module

--sleep: Hibernate specified number of seconds (can be decimal)

Import  Time Print (" start ") time.sleep (5) Print (" end ")
#如果在pycharm中输入这段代码, from the beginning to the end of the middle will stay for 5 seconds, sleep after the parentheses filled with the number of seconds

--time: Get timestamp

Import= time.time ()print(t)# This code is to get the timestamp ( From 1970-01-01 00:00:00 to the number of seconds you entered the code runtime)

--localtime: Converting a timestamp to an object

Import time
Local_time =print(local_time)# prints out the time of day (s) Local_time.tm_year)# Specify the output corresponding year print(local_time[0])# You can also specify the subscript output year

Mktime: Convert to timestamp based on date and year

Import time
New_time = Time.mktime ((2099, 9, 9, 1, 212, 0))# The above brackets are filled with custom time, to write enough 9 numbers print< /c3>(new_time)# output of 1970-01-01 00:00:00 to custom time in seconds

Gmtime: function with localtime

Import time
GT = time.gmtime ()print(GT)# outputs the current time of year/month day seconds

Timezone:0 time zone to your time zone in seconds (/3600 = hours)

Import time
Print(time.timezone/3600)# I'm in the East 8 zone, so the output is -8.0 .

Strftime: Formatting Time.struct_time objects to display

Import time
Local_time =time.localtime ()#formatted displayPrint(Time.strftime ('%y-%m-%d%h:%m:%s', Local_time))#format of the specified outputPrint(Time.strftime ('%d', Local_time))#\d is a specific display format month/day/year#%Y: Year (4-bit),%Y: Year (2-bit),%m: Month,%d: day,%d: month/day/year,%H: when#%M: Minutes,%s: sec,%w: Week (1~7),%w: This week is the first week of this year#a specific display formatPrint(Time.asctime ())#format output Current time

Python--time (Time) module

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.