Python Module--time and datetime

Source: Internet
Author: User

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 ()

Time Module

Import Timeprint (Time.time ())   #输出1495018452.927428print (Time.ctime ())  #输出Wed May 17 18:54:48 2017, current system time # Struct_time Object Print (Time.gmtime ()) #输出time. Struct_time (tm_year=2017, tm_mon=5, tm_mday=17, tm_hour=11, tm_min=0, tm_ sec=56, tm_wday=2, tm_yday=137, tm_isdst=0) #注意: The time of Gmtime is UTC, or Greenwich Mean time. After 8, it is Beijing time (East eight district). LocalTime is the time in this time zone. Print (Time.localtime ()) #输出time. Struct_time (tm_year=2017, tm_mon=5, tm_mday=17, tm_hour=19, tm_min=11, tm_sec=28, tm_ wday=2, tm_yday=137, tm_isdst=0) time_obj = Time.gmtime () print ("{0}-{1}-{2}". Format (Time_obj.tm_year, time_obj.tm_ Mon, Time_obj.tm_mday)) print (Time.mktime (Time.localtime ()))  #struct_time对象转为时间戳time. Sleep (3)   #程序延迟3秒, Then proceed to print (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))  #将struct_time格式转成指定的字符串格式print ( Time.strptime ("2010-5-18 15:06", "%y-%m-%d%h:%m"))    #将字符串格式转化成struct_time格式

  

DateTime module

Import Datetimeimport Timeprint (Datetime.date.today ()) #输出2017 -05-17print (Datetime.date.fromtimestamp () ))  #输出2017-05-17, convert timestamp to date format Current_time =datetime.datetime.now () print (current_time)    #输出2017-05-17 22:03:40.630623print (Current_time.timetuple ())  #返回strunt_time格式print (Current_time.replace (2015, 9, 10)) # Replaces the specified portion of the current time print (Datetime.datetime.now () + Datetime.timedelta (days=10))  #比现在加10天print (Datetime.datetime.now () + Datetime.timedelta (days=-10)) #比现在减10天print (Datetime.datetime.now () + Datetime.timedelta (hours=10)) # 10 hours more than now print (Datetime.datetime.now () + Datetime.timedelta (seconds=10)) #比现在加10秒print (Datetime.datetime.strptime ( ' 21/11/06 16:03 ', '%d/%m/%y%h:%m ')  #将字符串转为时间

  

Python Module--time and datetime

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.