Python Time and Datatime modules

Source: Internet
Author: User
Tags string format

Time and Datatime modules

Time-related operations, time is represented in three ways:

    • Timestamp after January 1, 1970 seconds, i.e.: Time.time ()
    • Formatted string 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')
    • Structured time tuples include: year, day, week, etc. time.struct_time is: time.localtime ()

Import time# return processor Time, 3.3 is obsolete, changed to Time.process_time () measurement processor time, not including sleep time, unstable, Mac on the test print (Time.clock ()) Print ( Time.process_time ()) input result: 1.520822339055174e-060.09360059999999999

Import time# Returns the time difference from UTC, in seconds, for print (time.altzone) input results: 32400

Import time# return time format "Thu Oct 17:26:06" Print (Time.asctime ()) input result: Thu 30 16:53:21 2017

Import time# Returns the struct time object format for local times print (Time.localtime ()) input result: Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=30 , Tm_hour=16, tm_min=53, tm_sec=21, tm_wday=3, tm_yday=334, tm_isdst=0)

Import time# return time format "Fri 11:14:16" Print (Time.asctime (Time.localtime ())) # Return time format "Fri-11:14:16" Print (Time.ctime ()) input result: Thu 16:53:21 2017Thu 30 16:53:21 2017

Import time# Returns the Struc Time Object format for UTC time Print (Time.gmtime (Time.time ()-800000)) input result: Time.struct_time (tm_year=2017, tm_mon= One, tm_mday=21, tm_hour=2, tm_min=40, Tm_sec=1, Tm_wday=1, tm_yday=325, tm_isdst=0)

Import time# # Date string to timestamp  # # Convert date string to struct time object Format string_2_struct = Time.strptime ("2016/06/22", "%y/%m/%d") print (s tring_2_struct) string_2_struct = Time.strptime ("2016.06.22", "%y.%m.%d") print (string_2_struct) input result: time.struct_ Time (tm_year=2016, tm_mon=6, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=174, Tm_isdst=-1) Time.struct_time (tm_year=2016, tm_mon=6, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=174, tm_isdst=- 1)

Import time# Convert the struct time object to timestamp Struct_2_stamp = Time.mktime (string_2_struct) print (struct_2_stamp) input result: 1466524800.0

Import time# # return time format "Fri 11:14:16" a = Time.asctime (string_2_struct) print (a) input result: Wed June 22 00:00:00 2016

Import time# convert timestamp to string format print (Time.gmtime (Time.time ()-86640) # converts the UTC timestamp to the struct_time format to enter the result: Time.struct_time (tm_ year=2017, tm_mon=11, tm_mday=29, Tm_hour=8, tm_min=49, tm_sec=21, tm_wday=2, tm_yday=333, tm_isdst=0)

Import Timeprint (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()) # converts the UTC struct_time format to the specified string format print ( Time.strftime ("%y:%m:%d%h:%m:%s", Time.gmtime (Time.time () +28800)) # Eight zone time print ("\033[32m%s\033[0m"% time.strftime ("%y:%m:%d%h:%m:%s")) # positive Eight zone time print ("\033[31m%s\033[0m"% time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) # Eight zone time input result: 2017-11-30 08:53:212017:11:30 16:53:212017:11:30 16:53:212017-11-30 16:53:21

Python Time and Datatime modules

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.