time模塊

 1 #!/urs/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import time  4 print(time.time())  5 # 返回時間戳
 6 print(time.sleep(5))
 7 # 線程推遲指定時間運行
 8 print(time.localtime())  9 # 返回本地時間 的struct_time對象格式
10 print(time.mktime(time.localtime())) 11 # 將一個時間對象(struct_time)轉成時間戳
12 print(time.ctime(time.time()))
# 把一個時間戳轉化成time.asctime的形式。
13 print(time.asctime(time.localtime())) 

14 # 把時間戳或時間對象(struct_time)轉成time.asctime()的形式

15 print("#"*80)

16 print(time.strftime("%y-%m-%d %X", time.localtime()))

17 print(time.strftime("%Y-%m-%d %X", time.gmtime()))

18 # time.strftime(format,t)把一個表明時間元組或者struct_time(time.gmtime()和time.localtime())轉化成時間字符串,t未指定,將傳入time.localtime()。

19 print("*"*80)

20 print(time.strptime("2018-03-26 07:57", "%Y-%m-%d %H:%M"))

21 # 把一個格式化時間字符串轉化成struct_tim
1522051889.3851166 None time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=16, tm_min=11, tm_sec=34, tm_wday=0, tm_yday=85, tm_isdst=0) 1522051894.0 Mon Mar 26 16:11:34 2018 Mon Mar 26 16:11:34 2018
################################################################################
18-03-26 16:11:34
2018-03-26 08:11:34
******************************************************************************** time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=7, tm_min=57, tm_sec=0, tm_wday=0, tm_yday=85, tm_isdst=-1)

時間戳:一般來講,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。time.time返回float類型。python

元組方式:struct_time元組共有9個元素,返回struct_time的函數有gmtime()   ,localtime(),  strptime().git

                   屬性                              值app

      tm_year (年)        2018ide

           tm_mon(月)        1-12函數

        tm_mdy(日)         1-30ui

                tm_hour(時)        0-23spa

         tm_min(分)        0-59線程

       tm_sec(秒)        0-60code

      tm_wday(周)              0-6orm

     tm_yday(一年中的第幾天)     1-366

     tm_isdst(是不是夏令時)    默認爲-1

 

常見格式:

Directive Meaning Notes
%a Locale’s abbreviated weekday name.  
%A Locale’s full weekday name.  
%b Locale’s abbreviated month name.  
%B Locale’s full month name.  
%c Locale’s appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal '%' character.

將時間戳轉爲字符串格式
print(time.gmtime(time.time()-86640)) #將utc時間戳轉換成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將utc struct_time格式轉成指定的字符串格式


日期字符串 轉成  時間戳
 string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #將 日期字符串 轉成 struct時間對象格式
 print(string_2_struct)

 struct_2_stamp = time.mktime(string_2_struct) #將struct時間對象轉成時間戳
 print(struct_2_stamp)



datetime模塊
 1 #!/urs/bin/evn python
 2 # -*- coding:utf-8 -*-
 3  #時間加減
 4 import datetime  5 import time  6 print(datetime.datetime.now())  7 # 返回2018-03-26 17:23:03.617732
 8 print(datetime.datetime.time(datetime.datetime.now()))  9 # 返回17:23:03.617732
10 print(datetime.time(3)) 11 # 00:00:00
12 print(datetime.date.fromtimestamp(time.time())) 13 # 時間戳直接轉成日期格式 2018-03-26
14 print(datetime.timedelta(3)) 15 # 返回3 days, 0:00:00
16 """def __new__(cls, days=None, seconds=None, microseconds=None, milliseconds=None, minutes=None, hours=None, weeks=None) 17 """
18 # 時間計算
19 print(datetime.datetime.now()+datetime.timedelta(3)) 20 # 加3天
21 print(datetime.datetime.now()-datetime.timedelta(3)) 22 # 減3天
23 print(datetime.datetime.now()-datetime.timedelta(hours=3)) 24 # 減3小時
25 print(datetime.datetime.now()+datetime.timedelta(minutes=3)) 26 # 加3分鐘
27 
28 # 時間替換
29 c_time  = datetime.datetime.now() 30 print(c_time.replace(minute=3, hour=2)) #時間替換
31 print(c_time.replace(2017, 5, minute=3, hour=2)) #時間替換
32 print(c_time.replace(2017,5,12, 2,44,23))
 
2018-03-26 17:52:35.131057
17:52:35.132057
03:00:00
2018-03-26
3 days, 0:00:00
2018-03-29 17:52:35.132057
2018-03-23 17:52:35.132057
2018-03-26 14:52:35.132057
2018-03-26 17:55:35.132057
2018-03-26 02:03:35.132057
2017-05-26 02:03:35.132057
2017-05-12 02:44:23.132057
相關文章
相關標籤/搜索