python筆記網站:http://www.runoob.com/python/python-tutorial.html ...
Python time strptime()方法
描述
Python time strptime() 函數根據指定的格式把一個時間字元串解析為時間元組。
語法
strptime()方法語法:
time.strptime(string[, format])
參數
- string -- 時間字元串。
- format -- 格式化字元串。
返回值
返回struct_time對象。
說明
python中時間日期格式化符號:
- %y 兩位數的年份表示(00-99)
- %Y 四位數的年份表示(000-9999)
- %m 月份(01-12)
- %d 月內中的一天(0-31)
- %H 24小時制小時數(0-23)
- %I 12小時制小時數(01-12)
- %M 分鐘數(00=59)
- %S 秒(00-59)
- %a 本地簡化星期名稱
- %A 本地完整星期名稱
- %b 本地簡化的月份名稱
- %B 本地完整的月份名稱
- %c 本地相應的日期表示和時間表示
- %j 年內的一天(001-366)
- %p 本地A.M.或P.M.的等價符
- %U 一年中的星期數(00-53)星期天為星期的開始
- %w 星期(0-6),星期天為星期的開始
- %W 一年中的星期數(00-53)星期一為星期的開始
- %x 本地相應的日期表示
- %X 本地相應的時間表示
- %Z 當前時區的名稱
- %% %號本身
實例
以下實例展示了 strptime() 函數的使用方法:
實例(Python 2.0+)
#!/usr/bin/python# -*- coding: UTF-8 -*-
import time
struct_time = time.strptime("30 Nov 00", "%d %b %y")
print "返回的元組: %s " % struct_time
以上實例輸出結果為:
返回的元組: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
python筆記網站:http://www.runoob.com/python/python-tutorial.html