1. 程式人生 > >pythopn time & datetime模塊(時間)

pythopn time & datetime模塊(時間)

日期時間 日期格 text ecs 當前時間 size 2.0 lac 參數

>>> import time >>> print(help(time)) # help查看python模塊中函數的用法 >>> print(time.time()) #1515773238.5295434 當前時間戳 >>> time.sleep(3) #推遲調用線程的運行,可通過參數secs指秒數,表示進程掛起的時間 >>> print(time.clock()) #計算CPU執行時間 >>> print(time.gmtime()) # 結構化時間 返回一個元組 >>> print(time.localtime()) #格式化時間戳為本地的時間 >>> print(help(time.strftime)) ************************* >>> localtime=time.localtime() >>> print(time.strftime(‘%Y-%m-%d %H-%M-%S‘,localtime)) >>> #ime.strftime 2018-01-13 00-34-45 接收以時間元組,並返回以可讀字符串表示的當地時間 ************************ >>> ztime=(‘2009-2-17 12-52-55‘) >>> a=time.strptime(ztime,"%Y-%m-%d %H-%M-%S") >>> print(a) #根據指定的格式把一個時間字符串解析為時間元組。 >>> print(a.tm_mon) #獲取指定日期時間 此項為獲取月 2 ************************** >>> print(time.ctime()) #Sat Jan 13 00:46:23 2018 默認當前時間戳 時間戳轉換成結構化時間 >>> print(time.ctime(1515773238.5295434)) #Sat Jan 13 00:07:18 2018 >>> print(time.mktime(time.localtime())) #1515775876.0 結構化時間轉換成時間戳 >>> import datetime >>> print(datetime.datetime.now()) #2018-01-13 00:54:12.721580 >>> print(datetime.date.fromtimestamp(time.time()) ) # 時間戳直接轉成日期格式 2018-01-13 >>> print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天 2018-01-16 13:27:11.574250 >>> print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天 2018-01-10 13:27:11.574250 >>> print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時 2018-01-13 16:27:11.574250 >>> print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分 2018-01-13 13:57:11.574250 >>> c_time = datetime.datetime.now() >>> print(c_time.replace(minute=3,hour=2)) #時間替換 2018-01-13 02:03:52.046024

技術分享圖片

pythopn time & datetime模塊(時間)