6, module detailed two time module, datetime module

Source: Internet
Author: User
Tags timedelta

1. Time Module

Time.time () #返回时间戳

Print (Time.time ()) 1465609581.970584

Time.ctime (seconds) #返回指定时间戳的系统日期

Print (Time.ctime ())    Sat June 11 09:47:58 2016# parameter: timestamp, default now print (Time.ctime) Thu Jan 1 08:00:23 1970

  

Time.gmtime (seconds) #返回struct时间格式, default GMT

Print (Time.gmtime ())  time.struct_time (tm_year=2016, tm_mon=6, tm_mday=11, Tm_hour=1, tm_min=51, tm_sec=18, tm_ Wday=5, tm_yday=163, tm_isdst=0) #参数: timestamp, default now print (Time.gmtime ()) Time.struct_time (tm_year=1970, Tm_mon=1, Tm_mday =1, tm_hour=0, tm_min=0, tm_sec=23, tm_wday=3, Tm_yday=1, tm_isdst=0) #单独显示年月日, return the value of the Int type print (Time.gmtime (). tm_ Year) 2016print (Time.gmtime (). tm_year-1) 2015print (Time.gmtime (). Tm_mon) 06print (Time.gmtime (). Tm_mday) 9

  

Time.localtime () #以struct的格式返回本地时间

Print (Time.localtime ()) Time.struct_time (tm_year=2016, tm_mon=6, tm_mday=11, tm_hour=10, tm_min=4, tm_sec=37, Tm_wday =5, tm_yday=163, tm_isdst=0) #单独显示年月日, return a value of type int print (Time.gmtime (). Tm_year) 2016print (Time.gmtime (). tm_year-1) 2015print (Time.gmtime (). Tm_mon) 06print (Time.gmtime (). tm_mday) 9# minutes tm_hour,tm_min,tm_sec# weeks to Tm_wday  

Time.localtime (p_tuple) #将struct格式的时间转换成时间戳

Print (Time.mktime (Time.localtime ())) 1465611464.0# parameter: Time in p_tuple:struct format

Time.strftime (format,p_tuple) #将struct格式的时间用format给定的格式打印

Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) 2016-06-11 10:21:20# parameter 1 format  : Formatting for the given time # parameter 2  p_ Time in tuple:struct format

Time.strptime (String,format) #将制定format格式的string转换成struct格式的时间

Print (Time.strptime (' 2016-06-05 ', '%y-%m-%d '))  time.struct_time (tm_year=2016, tm_mon=6, tm_mday=5, tm_hour=0, Tm_min=0, Tm_sec=0, tm_wday=6, tm_yday=157, tm_isdst=-1) #参数1  string: The given date # parameter 2  format: Formatting of the given date

2. DateTime module

Datetime.date.today () #返回当前日期, format 2016-06-05

Print (Datetime.date.today ()) 2016-06-11

Datetime.date.fromtimestamp (cls,timestamp) #将时间戳转换成 xx-xx-xx format and returns

Print (Datetime.date.fromtimestamp (Time.time ())) 2016-06-11# parameter  timestamp: timestamp

Datetime.datetime.now () #返回当前时间2016-06-05 18:23:30.819466, time plus minus

Print (Datetime.datetime.now ())     2016-06-11 10:57:57.825386new_date = Datetime.datetime.now () + Datetime.timedelta (days=10)     #比现在加10天new_date = Datetime.datetime.now () + Datetime.timedelta (days=-10)    # Minus 10 days than now new_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10)   #比现在减10小时new_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120) #比现在 +120s

Time comparison: True if a match is returned

Current_time= Datetime.datetime.now () time_obj = Current_time.replace (2014,9,12) print (current_time>time_obj)                                #date时间可以相互比较print (str (current_time) > ' 2014-09-12 18:37:26.879790 ')     #可以比较

  

6, module detailed two time module, datetime 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.