Python Time Module Learning

Source: Internet
Author: User

The time module in Python can be used as a format for the timing display, with a flexible and powerful function. There are a lot of parameters in the Time.strftime that will allow you to more freely output what you want: Here is the Time.strftime parameter: strftime (format[, tuple]), string to specify the Struct_ Time (the default is the current time, the tuple type), based on the specified formatted string output, returns the result as a string in Python in the date format symbol:%y Two-digit year representation (00-99)%y Four-digit year representation (000-9999)%m month (01-12) One day in%d months (0-31)%H 24-hour hours (0-23)%I 12-hour hours (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 corresponding date representation and time representation%j One day of the year (001-366)%p local a.m. or p.m. the number of weeks in the year (00-53) Sunday for the beginning of the week%w Weeks (0-6), Sunday for the beginning of the week%w the week of the Year (00-53) Monday for the beginning of the week%x Local corresponding date indicates%x local corresponding time represents%z the name of the current time zone% percent of itself below the collection of several examples below is explained below: code example 1:#!/usr/bin/python#-*-coding:utf-8-*-import timeprint " Time.time ():%f "% time.time () #返回当前时间的时间戳 (the number of floating-point seconds elapsed since the 1970 era). Print Time.localtime (Time.time ())

The output of the above code:

Time.time (): 1234892919.655932
(2009, 2, 17, 10, 48, 39, 1, 48, 0)

code Example 2:

#!/usr/bin/env python

#-*-Coding:utf-8-*-

Import time


TI = Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))
Print Ti

The output of the above code is:

2015-02-11 14:54:02

code Example 3:

#!/usr/bin/env python

#-*-Coding:utf-8-*-

Import time


Print Time.time ()
Print Time.localtime ()
Print Time.localtime (Time.time ())
Print time.strftime ('%y-%m-%d ', Time.localtime ())
Print time.strftime ('%y-%m-%d ', Time.localtime ())
Print Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ())
Print Time.strftime ('%y-%m-%d%i:%m:%s ', Time.localtime ())
Print Time.strftime ('%y-%m-%d%h:%m:%s--%a--%c ', Time.localtime ())
Print Time.strftime ('%y-%m-%d%h:%m:%s--%x--%x ', Time.localtime ())

The output of the above code is:

1423638447.02
Time.struct_time (tm_year=2015, tm_mon=2, tm_mday=11, tm_hour=15, tm_min=7, tm_sec=27, tm_wday=2, tm_yday=42, tm_isdst= 0)
Time.struct_time (tm_year=2015, tm_mon=2, tm_mday=11, tm_hour=15, tm_min=7, tm_sec=27, tm_wday=2, tm_yday=42, tm_isdst= 0)
2015-02-11
15-02-11
2015-02-11 15:07:27
2015-02-11 03:07:27
2015-02-11 15:07:27--wednesday--wed Feb 11 15:07:27 2015
2015-02-11 15:07:27--02/11/15--15:07:27


#me: If you want to specify the output, refer to the parameters given above! You can then write these times to the log file, complete the logging function!





Python Time Module Learning

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.