Python's built-in function time

Source: Internet
Author: User
Tags local time string format

Time Module
1 >>> Import time 2 >>> time.time () 3 1491064723.808669 4 >>> # Time.time () returns the timestamp of the current time timestamp (  Defined as the total number of seconds from GMT January 01, 1970 00:00 00 seconds to present, no parameters 5 >>> time.asctime () 6 ' Sun Apr 2 00:39:32 ' 7 >>> #  Time.asctime () Convert Struct_time object format to string format ' Sun Apr 2 00:39:32 ' 8 >>> time.asctime (Time.gmtime ()) 9 ' Sat APR 1 16:41:41 >>> Time.asctime (Time.localtime ()) One ' Sun Apr 2 00:42:06 ' >>> time.ctime (13) ' Sun Apr 2 00:42:29 >>> # time.ctime () convert timestamp to string format ' Sun Apr 2 00:42:29 2017 ', default current timestamp [Tim] E.ctime (1491064723.808669) ' Sun Apr 2 00:38:43 ' >>> Time.altzone # Returns the time difference from UTC, in seconds 18-3240019 >&G T;> Time.localtime () # Converts the timestamp to the Struct_time object format, returning the current timestamp by default (tm_year=2017, tm_mon=4, tm_mday=2, tm_ Hour=0, tm_min=45, tm_sec=26, tm_wday=6, tm_yday=92, tm_isdst=0) >>> time.localtime (1491064723.808669) 22 Time.struct_time (tm_year=2017, tm_mOn=4, tm_mday=2, tm_hour=0, tm_min=38, tm_sec=43, tm_wday=6, tm_yday=92, tm_isdst=0) >>> >>> time. Gmtime () # converts the UTC timestamp to the Struct_time object format, returning the current time by default to Time.struct_time (tm_year=2017, tm_mon=4, Tm_mday=1, tm_hour=16, tm_ Min=46, tm_sec=32, tm_wday=5, tm_yday=91, tm_isdst=0) >>> time.gmtime (1491064723.808669) time.struct_ Time (tm_year=2017, tm_mon=4, Tm_mday=1, tm_hour=16, tm_min=38, tm_sec=43, tm_wday=5, tm_yday=91, tm_isdst=0) >> > >>> >>> time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()) # The local time struct_ The time format is converted to a custom string format 2017-04-01 23:15:4731 ' 2017-04-02 00:47:49 ' >>> >>> time.strftime ('%y-%m-%d%H: %m:%s ', Time.gmtime () # convert UTC time Struct_time format to custom string format 2017-04-01 23:15:4734 ' 2017-04-01 16:48:27 ' >>> &G T;>> time.strptime (' 2017-04-02 00:47:49 ', '%y-%m-%d%h:%m:%s ') # Convert date string to struct_time time object format, note converted Tm_isdst=-1 () Daylight saving Time status PNs time.struct_time (tm_year=2017, tm_mon=4, Tm_mday=2, TM_hour=0, tm_min=47, tm_sec=49, tm_wday=6, tm_yday=92, tm_isdst=-1) >>> >>> time.mktime ( Time.localtime ()) 1491065416.041 >>> # Turns the Struct_tiame time object into a timestamp result returns 1491061855.0, ignoring the time less than the second (after the decimal point) > >> >>> time.mktime (Time.localtime (1491061855.0011407)) 1491061855.045 >>> # The result returns 1491061855.0, ignoring the time less than seconds (ignoring the decimal point). >>> >>> Time.mktime (Time.gmtime (1491061855.0011407)) 48 1491033055.049 >>> >>> # results return 1491033055.0, ignoring time less than seconds (ignoring the decimal point) or >>>

  

Format characters and meanings

%a Week's shorthand. As Wednesday for web
%A week's full write. As in Wednesday for Wednesday
Abbreviated%B month. If April is Apr
Full write of%b month. As in April for April
%c: string representation of DateTime. (Example: 04/07/10 10:43:39)
%d: The number of days in the month (the day of the week)
%f: microseconds (range [0,999999])
%H: Hours (24-hour, [0, 23])
%I: Hours (12-hour, [0, 11])
%j: Number of days in the year [001,366] (the day of the current year)
%m: Month ([01,12])
%M: Minutes ([00,59])
%p:am or PM
%s: seconds (range = [00,61], why not [00, 59], refer to Python manual ~_~)
%u: Week in the Year of the Week of the year), Sunday as the first day of the week
%w: Today in this week's days, the range is [0, 6],6 represents Sunday
%W: Week of the Year (the Week of the year), Monday as the first day of the week
%x: Date string (for example: 04/07/10)
%x: Time string (for example: 10:43:39)
%y:2 the year represented by a number
%y:4 the year represented by a number
%z: Interval with UTC time (if local time, returns an empty string)
%Z: Time zone name (if local time, returns an empty string)

Python's built-in function time

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.