Python Time module

Source: Internet
Author: User

Import time after calling Time.gmtime () to get standard times

Time.struct_time (tm_year=2016,tm_mon=8,tm_mday=20,tm_hour=6,tm_min=0, tm_sec=0, tm_wday=3, tm_yday= 1, tm_isdst=0)

Call Time.localtime () to get local time (East zone eight)

Time.struct_time (tm_year=2016, tm_mon=8, tm_mday=20, tm_hour=14, tm_min=0, tm_sec=0, tm_wday=3, Tm_yday=1, tm_isdst=0)

Call Time.localtime (1231231322)

Time.struct_time (tm_year=2009, Tm_mon=1, tm_mday=6, tm_hour=16, tm_min=42, tm_sec=2, Tm_wday=1, tm_yday=6, tm_isdst=0)

Compared to 1231231322/3600/24/365 = 39.0421, 1970+39 = 2009

###############################################################################

Struct_time conversion to Timestamp

Time.mktime (x) #x为struct_time格式, for example:

1 Import  Time 2 x = Time.localtime (1231231322)3print(time.mktime (x))

Output:

1231231322.0

###############################################################################

Struct_time Convert to formatted time

Time.strftime ("%y-%m-%d%h:%m:%s", x) #x为struct_time格式, separators can be customized, generally used-or:

Output: 2009-01-06 16:42:02

###############################################################################

Format time converted to Struct_time

Time.strptime (' 2016-08-20 14:31:52 ', "%y-%m-%d%h:%m:%s")

Output: Time.struct_time (tm_year=2016, Tm_mon=1, Tm_mday=8, tm_hour=14, tm_min=31, tm_sec=52, tm_wday=4, Tm_yday=8, tm_ ISDST=-1)

###############################################################################

Struct_time converted to Acstime (local time with week and month abbreviation)

Time.asctime (x) #x为struct_time格式

Output: Tue Jan 6 16:42:02 2009

###############################################################################

Timestamp converted to CTime (local time with week and month abbreviation)

Time.ctime (1231231322)

Output: Tue Jan 6 16:42:02 2009

###############################################################################

There is also a datetime module

Datetime.date Date (Month day)

Datetime.time time (hours, minutes, seconds)

Datetime.datetime Date + Time

Datetime.datetime.now () #当前时间

Datetime.datetime.now () + Datetime.timedelta ( -3) #三天前时间

Datetime.datetime.now () + Datetime.timedelta (hours=3) #三小时后时间

Datetime.datetime.now () + Datetime.timedelta (minutes=-2) #两分钟前时间

Datetime.datetime.now () + Datetime.timedelta (Seconds=1) #1秒钟后时间

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.