Learning Tour of Python ——— time module

Source: Internet
Author: User
Tags timedelta

In Python, there are usually several ways to represent time:

    • Timestamp (timestamp): The timestamp represents the offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.
    • Formatted time string (format string)
    • Structured time (Struct_time): Struct_time A total of 9 elements in total nine elements: (year, month, day, hour, minute, second, week of the year, Day of the year, summer Time)

Between them can be converted

                          Time.localtime () If you do not add seconds, the default is T Ime.time ()       tuple                                                    ,         &N Bsp             time.localtime  default can not fill in

                           time.gmtime () UTC time &N Bsp       tuples                                                      ,         &NB Sp                          ,         &NB Sp          time.strftime ("%y-%m-%d%x", Time.localtime ())     string

Timestamp    ---------------------------------------->                & nbsp: Structured time                    ------------------------------------- >  format time                 <---------------------------------------                          ,         &NB Sp                   <--------------------------------------

Time.mktime () floating point time.strptime (' 2011-05-05 1 6:37:06 ', '%y-%m-%d%x ') tuples

both structural and timestamp can be converted to ' Sun June 20 23:21:05 1993 ' In this form of string.

Parameter structure asctime ([t]) if there are no parameters, the Time.localtime () will be passed in as a parameter

When the parameter timestamp ctime ([secs]) is none, the default Time.time () is the argument.

DateTime module

1 #time plus minus2 Importdatetime3 4 #print (Datetime.datetime.now ()) #返回 2016-08-19 12:47:03.9419255 #Print (Datetime.date.fromtimestamp (Time.time ())) # Timestamp goes directly to the date format 2016-08-196 #print (Datetime.datetime.now ())7 #print (Datetime.datetime.now () + Datetime.timedelta (3)) #当前时间 + 3 days8 #print (Datetime.datetime.now () + Datetime.timedelta ( -3)) #当前时间-3 days9 #print (Datetime.datetime.now () + Datetime.timedelta (hours=3)) #当前时间 + 3 hoursTen #print (Datetime.datetime.now () + Datetime.timedelta (minutes=30)) #当前时间 +30 points One  A  - # - #c_time = Datetime.datetime.now () the #Print (C_time.replace (minute=3,hour=2)) #时间替换 -  -DateTime module

Learning Tour of Python ——— time module

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.