日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python日期函数_python 时间相关函数

發布時間:2024/4/13 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python日期函数_python 时间相关函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python 中與時間處理相關的模塊包括 time、datetime、以及 calendar

time 模塊

time() 函數:time() 函數用于返回當前時間的時間戳(1970年01月08時00分00秒到現在的浮點秒數)

time() 函數的語法:time.time() //此語句中的第一個 time 指的是 time 模塊,該函數參數列表為空

代碼:

1 importtime2

3 print('當前時間的時間戳:%f' % time.time())

View Code

localtime([secs]) 函數

localtime() 函數的作用是格式化時間戳為本地 struct_time。如果 secs 參數未輸入則默認為當前時間

localtime() 函數語法:time.localtime([secs]) //time指的是 time 模塊,可選參數 secs 表示 1970-1-1 到現在的秒數

代碼:

1 importtime2

3 print('time.localtime():', time.localtime())4 #輸出:

5 #time.localtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=15, tm_min=20, tm_sec=12, tm_wday=1, tm_yday=107, tm_isdst=0)

6 #最后三個輸出的值的含義為 一周的第幾日 0 ~ 6(0 是周一),一年中的第幾日,夏令時(-1, 0, 1, -1)

View Code

gmtime([secs]) 函數

gmtime() 函數用于將一個時間戳轉換成 UTC 時區(0 時區)的 struct_time,可選參數 secs 表示 1970-1-1 到現在的秒數,若 secs 參數未輸入則默認為當前時間

gmtime() 函數的語法: time.gmtime([secs])

代碼:

1 importtime2

3 print('time.gmtime():', time.gmtime())4 #輸出:

5 #time.gmtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=7, tm_min=32, tm_sec=54, tm_wday=1, tm_yday=107, tm_isdst=0)

View Code

mktime(t) 函數

mktime() 函數用于執行與 gmtime()、localtime() 相反的操作,接收 struct_time 對象作為參數,返回用秒表示時間的浮點數。如果輸入不合法的時間則觸發 OverflowError 或 ValueError 異常

代碼:

1 importtime2

3 t = (2018, 4, 17, 15, 20, 12, 1, 107, 0);4 print('time.mktime(t): %f' %time.mktime(t))5 #輸出:

6 #time.mktime(t): 1523949612.000000

View Code

asctime([t]) 函數

asctime() 函數用于接收時間元組(struct_time)并返回一個可讀形式為 Tue Apr 17 15:45:13 2018 的 24 個字符的字符串

部輸出參數 t 則默認為當前時間點

代碼:

1 importtime2

3 t =time.localtime()4 print('time.asctime(t): %s' %time.asctime(t))5 #輸出:

6 #time.asctime(t): Tue Apr 17 15:45:13 2018

View Code

ctime([secs]) 函數

ctime 函數用于將一個時間戳轉化為 time.asctime() 的形式。若為指定參數 secs 則默認將 time.time() 作為參數

代碼:

1 importtime2

3 print('time.ctime(): %s' %time.ctime())4 #輸出:

5 #time.ctime(): Tue Apr 17 15:51:00 2018

View Code

sleep(secs) 函數

sleep() 函數用于推遲調用線程的運行,可通過參數 secs 指定進程掛起的時間

sleep() 函數語法:time.sleep(escs) //其中 time 是指 time 模塊,secs 指推遲執行的秒數,此函數沒有返回值

1 importtime2

3 print('start: %s' %time.ctime())4 time.sleep(5)5 print('end: %s' %time.ctime())6 #輸出:

7 #start: Tue Apr 17 16:00:16 2018

8 #end: Tue Apr 17 16:00:21 2018

View Code

clock() 函數

clock() 函數用于以浮點數計算的秒數返回當前 cpu 時間,用來衡量不同程度程序的耗時。該函數在不同的系統上含義不同。在 UNIX 系統上,返回的是 “進程時間”,是用秒表示的浮點數(時間戳)。在 Windows 中,第一次調用返回的是進程運行的實際時間,第二次之后的調用返回的是自第一次調用后到現在的運行時間

代碼:

1 importtime2

3 defprocedure():4 time.sleep(2)5

6 #measure process time

7 procedure()#sleep的時間不算程序運行的時間,sleep()是沒有占用cpu

8 t1 =time.clock()9 print(t1)#程序運行的時間

10 procedure()11 print('seconds process time:', time.clock())#第一次調用clock到本次調用clock的時間間隔

12

13 #measure wall time

14 t2 =time.time()15 procedure()16 print('seconds wall time:', time.time() -t2)17 #輸出:

18 #0.0

19 #seconds process time: 2.0004229494329526

20 #seconds wall time: 2.000399351119995

View Code

strftime(format[, t]) 函數

用于接收時間 struct_time 元組,并返回可讀字符串表示的當地時間,格式由參數 format 決定

time 指的是 time 模塊,format 指格式化字符串,t 指可選的參數,是一個 struct_time 對象

代碼:

1 importtime2

3 t = (2016, 9, 25, 17, 50, 38, 6, 48, 0)4 t = time.mktime(t)#將 struct_time 轉換成浮點秒數

5 print(time.strftime('%b %d %Y %H : %M : %S', time.gmtime(t)))#gmtime()將時間戳轉換成0時區struct_time

6 #輸出:

7 #Sep 25 2016 09 : 50 : 38

View Code

strptime(string[, format]) 函數

strptime 函數用于根據指定的格式把一個時間字符串解析成時間元組 struct_time,和 strftime 的作用剛好相反

其中 time 指的是 time 模塊,string 指時間字符串,format 指格式化字符串

代碼:

1 importtime2

3 struct_time = time.strptime("25 Sep 16", "%d %b %y")4 print(struct_time)5 #輸出:

6 #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=269, tm_isdst=-1)

View Code

datetime 模塊

today()

語法:datetime.datetime.today() //其中 datetime.datime 指的是 datetime.datime 類

代碼:

1 importdatetime2

3 print('today is:', datetime.datetime.today())4 #輸出:

5 #today is: 2018-04-19 19:38:59.625746

View Code

now([tz])

語法: datetime.datetime.now([tz]) // 后去 tz 參數所指時區的本地時間

代碼:

1 importdatetime2

3 print('now is:', datetime.datetime.now())4 #輸出:

5 #now is: 2018-04-19 19:42:43.731264

View Code

datetime.utcnow()

語法:datetime.datetime.utcnow() //返回一個當前 utc 時間的 datetime 對象

代碼:

1 importdatetime2

3 print('utcnow is:', datetime.datetime.utcnow())4 #輸出:

5 #utcnow is: 2018-04-19 11:46:16.801027

View Code

fromtimestmap(timestamp[, tz])

根據時間戳創建一個 datetime 對象。其中 tz 指定時區信息。返回一個 datetime 對象

代碼:

1 importdatetime, time2

3 print('fromtimestamp is:', datetime.datetime.fromtimestamp(time.time()))4 #輸出:

5 #fromtimestamp is: 2018-04-19 19:50:01.279816

View Code

utcfromtimestamp(timestamp)

根據時間戳創建一個 datetime 對象。其中,timestamp 指時間戳,返回一個 datetime 對象

代碼:

1 importdatetime, time2

3 print('utcfromtimestamp is:', datetime.datetime.utcfromtimestamp(time.time()))4 #輸出:

5 #utcfromtimestamp is: 2018-04-19 11:54:30.934395

View Code

strptime(date_string, format)

將格式字符串轉換成 datetime 對象

其中 date_string 指日期字符串,format 為格式化方式,返回一個 datetime 對象

代碼:

1 importdatetime2

3 dt =datetime.datetime.now()4 print('strptime is:', dt.strptime(str(dt), '%Y-%m-%d %H:%M:%S.%f'))5 #輸出:

6 #strptime is: 2018-04-19 20:07:49.020946

View Code

strftime(format)

將格式化字符串轉換為 datetime 對象

其中 format 為格式化方式

代碼:

1 importdatetime2

3 dt =datetime.datetime.now()4 print('strftime is:', dt.strftime('%Y-%m-%d %H:%M:%S'))5 #輸出:

6 #strftime is: 2018-04-19 20:11:17

View Code

calendar 模塊(日歷)

calendar.calendar(year, w = 2, l = 1, c = 6)

該函數返回一個多行字符串格式的 year 年歷,3 個月一行,間隔距離為 c。每日寬度間隔為 w 字符。每行長度為 21 * w + 18 + 2 * c。l 是每星期行數

代碼:

1 importcalendar2

3 print(calendar.calendar(2018, w = 2, l = 1, c = 6))4 '''

5 輸出:6 20187

8 January February March9 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su10 1 2 3 4 5 6 7 1 2 3 4 1 2 3 411 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 1112 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 1813 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 2514 29 30 31 26 27 28 26 27 28 29 30 3115

16 April May June17 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su18 1 1 2 3 4 5 6 1 2 319 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 1020 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 1721 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 2422 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 3023 3024

25 July August September26 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su27 1 1 2 3 4 5 1 228 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 929 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 1630 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 2331 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 3032 30 3133

34 October November December35 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su36 1 2 3 4 5 6 7 1 2 3 4 1 237 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 938 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 1639 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 2340 29 30 31 26 27 28 29 30 24 25 26 27 28 29 3041 3142 '''

View Code

calendar.isleap(year)

如果是閏年就返回 True,否則返回 False

代碼:

1 importcalendar2

3 print(calendar.isleap(2018))4 #輸出:

5 #False

View Code

calendar.leapdays(y1, y2)

返回 y1, y2 之間的閏年總數

總結

以上是生活随笔為你收集整理的python日期函数_python 时间相关函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。