Python模塊 - time,datetime,calendar

time 模塊python

time模塊方法:git

複製代碼
>>> import time
>>> time.time()               #時間戳 秒級別 
1519212085.6211221            #從1970年到如今一共度過1519212085秒;
>>> time.time()/3600/24/365   #48年 1970-2018年有這麼多秒 1h=3600s  
48.17390346104816 
>>> time.localtime()          #本地時間
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=20, tm_min=8, tm_sec=33, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> time.localtime(2342342434)
time.struct_time(tm_year=2044, tm_mon=3, tm_mday=23, tm_hour=18, tm_min=40, tm_sec=34, tm_wday=2, tm_yday=83, tm_isdst=0)
>>> a=time.localtime()           
>>> a
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=19, tm_min=28, tm_sec=45, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> '%s-%s-%d'%(a.tm_year,a.tm_mon,a.tm_mday)
'2018-2-21' 
>>> time.mktime(a)            #把localtime()時間反轉時間成時間戳           
1519214861.0 
複製代碼
複製代碼
0  tm_year    #年 1970-2018
1  tm_mon     #月 1-12
2  tm_mday    #日 1-31
3  tm_hour    #時 0-23
4  tm_min     #分 0-59
5  tm_sec     #秒 0-59
6  tm_wday    #一週中得第幾天 0-6
7  tm_yday    #一年中得第幾天 0-365
8  tm_isdst   #是不是夏令時  0-1
複製代碼
複製代碼
>>> time.gmtime()     #比北京時間晚8點 UTC時區(0時區) 
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=11, tm_min=50, tm_sec=46, tm_wday=2, tm_yday=52, tm_isdst=0)
>>> time.sleep(2)     #睡一會 單位爲 秒 
>>> time.asctime()    #表示時間得另一種方法
'Wed Feb 21 20:53:43 2018' 
>>> time.ctime()      #表示時間得另一種方法 
'Wed Feb 21 20:57:16 2018'
>>> time.ctime(3123)
'Thu Jan  1 08:52:03 1970'
>>> time.ctime(0)
'Thu Jan  1 08:00:00 1970'
>>> time.strftime('%Y-%m-%d %H:%M:%S')      #表示時間 字符串
'2018-02-21 21:04:48'
>>> a=time.localtime(2432423423)
>>> a
time.struct_time(tm_year=2047, tm_mon=1, tm_mday=30, tm_hour=9, tm_min=10, tm_sec=23, tm_wday=2, tm_yday=30, tm_isdst=0)
>>> time.strftime('%Y-%m-%d %H:%M:%S',a)     #表示某一時間戳得 字符串
'2047-01-30 09:10:23'
>>> time.strftime('%Y-%m-%d %H:%M:%S %a',a)  #a 周幾
'2047-01-30 09:10:23 Wed'
>>> time.strftime('%Y-%m-%d %H:%M:%S %A',a)  #A 周幾
'2047-01-30 09:10:23 Wednesday'
>>> time.strftime('%y-%m-%d %H:%M:%S %b')    #b 月
'18-02-21 21:22:19 Feb'
>>> time.strftime('%y-%m-%d %H:%M:%S %B')    #B 月
'18-02-21 21:22:29 February'
>>> time.strftime('%Y-%m-%d %H:%M:%S %p',a)  #p 上午下午
'2047-01-30 09:10:23 AM'
>>> time.strftime('%Y-%m-%d %H:%M:%S %U')    #U 一年得第幾周
'2018-02-21 21:08:15 07'
>>> time.strftime('%Y-%m-%d %H:%M:%S %w')    #w 一週得第幾天
'2018-02-21 21:09:55 3'
>>> time.strftime('%y-%m-%d %H:%M:%S %z')    #z 東8區時間
'18-02-21 21:10:38 +0800'
>> time.strftime('%Y-%m-%d %H:%M:%S %Z')     #Z 時間標準 
'2018-02-21 21:28:22 中國標準時間'

>>> s=time.strftime('%Y-%m-%d %H:%M:%S')     #時間字符串 格式  時間戳來回轉換 
>>> s
'2018-02-21 21:32:19'
>>> s2= time.strptime(s,'%Y-%m-%d %H:%M:%S')
>>> s2
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=32, tm_sec=19, tm_wday=2, tm_yday=52, tm_isdst=-1)
>>> time.mktime(s2)
1519219939.0
複製代碼
複製代碼
字符串轉時間格式對應表
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.
複製代碼

--------------------------------------app

總結:time模塊
1.time.time()
2.time.localtime()
3.time.localtime(2342342434)
4.time.mktime(a)
5.time.gmtime()
6.time.sleep(2)
7.time.asctime()
8.time.ctime()
9.time.ctime(3123)
10.time.strftime('%Y-%m-%d %H:%M:%S')
11.time.strftime('%Y-%m-%d %H:%M:%S',a)
12.time.strftime('%Y-%m-%d %H:%M:%S %A',a)
13.time.strptime(s,'%Y-%m-%d %H:%M:%S')ide

 -----------------------------------------------------------post

datetime模塊ui

相比於time模塊 datetime模塊得接口更直觀更容易調用 重點是進行時間運算this

複製代碼
方法:
>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2018, 2, 21, 21, 59, 57, 526811)
>>> a.year
2018
>>> a.timetuple()
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=59, tm_sec=57, tm_wday=2, tm_yday=52, tm_isdst=-1)
>>> d2=datetime.date.fromtimestamp(time.time())       #根據時間戳快速拿到年月日
>>> d2
datetime.date(2018, 2, 21)
>>> d2.timetuple()        #注意 時分秒 丟了
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=52, tm_isdst=-1)

時間運算:
>>> datetime.datetime.now()-datetime.timedelta(days=1)
datetime.datetime(2018, 2, 20, 22, 13, 4, 891830)
>>> datetime.datetime.now()-datetime.timedelta(days=3)
datetime.datetime(2018, 2, 18, 22, 14, 7, 771268)
>>> datetime.datetime.now()-datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 21, 19, 14, 33, 758609)
>>> datetime.datetime.now()+datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 22, 1, 14, 48, 426850)
>>> datetime.datetime.now()+datetime.timedelta(minutes=10)
datetime.datetime(2018, 2, 21, 22, 25, 32, 615892)
>>> datetime.datetime.now()+datetime.timedelta(seconds=10)
datetime.datetime(2018, 2, 21, 22, 16, 29, 661140) 

時間替換:
>>> s=datetime.datetime.now()
>>> s
datetime.datetime(2018, 2, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016)
datetime.datetime(2016, 2, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016,month=8)
datetime.datetime(2016, 8, 21, 22, 21, 34, 62949)
>>> s.replace(year=2016,month=8,day=2)
datetime.datetime(2016, 8, 2, 22, 21, 34, 62949) 
複製代碼

 

calendar是一個定義了一個日曆類,,封裝了一個月或年的日期和星期,另外,也能夠展現文本日曆和HTML日曆的格式輸出
import calendar  
  
c = calendar.TextCalendar(calendar.SUNDAY)  
c.prmonth(2016, 1)  
  
    January 2016  
Su Mo Tu We Th Fr Sa  
                1  2  
 3  4  5  6  7  8  9  
10 11 12 13 14 15 16  
17 18 19 20 21 22 23  
24 25 26 27 28 29 30  
31  
也能夠返回HTML
c = calendar.HTMLCalendar(calendar.SUNDAY)  
print(c.formatmonth(2016, 1))  
運行結果
<table border="0" cellpadding="0" cellspacing="0" class="month">  
<tr><th colspan="7" class="month">January 2016</th></tr>  
<tr><th class="sun">Sun</th><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th></tr>  
<tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="fri">1</td><td class="sat">2</td></tr>  
<tr><td class="sun">3</td><td class="mon">4</td><td class="tue">5</td><td class="wed">6</td><td class="thu">7</td><td class="fri">8</td><td class="sat">9</td></tr>  
<tr><td class="sun">10</td><td class="mon">11</td><td class="tue">12</td><td class="wed">13</td><td class="thu">14</td><td class="fri">15</td><td class="sat">16</td></tr>  
<tr><td class="sun">17</td><td class="mon">18</td><td class="tue">19</td><td class="wed">20</td><td class="thu">21</td><td class="fri">22</td><td class="sat">23</td></tr>  
<tr><td class="sun">24</td><td class="mon">25</td><td class="tue">26</td><td class="wed">27</td><td class="thu">28</td><td class="fri">29</td><td class="sat">30</td></tr>  
<tr><td class="sun">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr>  
</table>  

 

總結:spa

1.方法:a=datetime.datetime.now() a.year a.timetuple() d2=datetime.date.fromtimestamp(time.time()) 
2.時間運算:datetime.datetime.now()-datetime.timedelta(days=3)
3.時間替換:s=datetime.datetime.now() s.replace(year=2016,month=8)code

import time

print(time.time()) #用於計算
print(time.localtime(time.time()))
print(time.gmtime(time.time()+28800))
print(time.mktime(time.localtime())) #格式化-->時間戳
print(time.strftime('%Y-%m-%d %X',time.localtime())) #格式化--》字符串時間
print(time.strftime('%F %X',time.localtime()))
print(time.strptime('2017-01-03 09:37:06','%Y-%m-%d %X')) #字符串--》格式化
print(time.ctime())
E:\Python35\python.exe E:/time複習.py
1523599614.114538
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=13, tm_hour=14, tm_min=6, tm_sec=54, tm_wday=4, tm_yday=103, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=13, tm_hour=14, tm_min=6, tm_sec=54, tm_wday=4, tm_yday=103, tm_isdst=0)
1523599614.0
2018-04-13 14:06:54
2018-04-13 14:06:54
time.struct_time(tm_year=2017, tm_mon=1, tm_mday=3, tm_hour=9, tm_min=37, tm_sec=6, tm_wday=1, tm_yday=3, tm_isdst=-1)
Fri Apr 13 14:06:54 2018

Process finished with exit code 0
相關文章
相關標籤/搜索