本文目錄一覽:
- 1、python中,怎麼把字符串轉換為日期格式
- 2、python中,怎麼把字符串轉換為日期格式
- 3、python中如何把datetime.datetime轉換成datetime.time?
- 4、新手求教:python 時間格式轉換
- 5、python的日期類型轉換
python中,怎麼把字符串轉換為日期格式
1、python中要把字符串轉換成日期格式需要使用time模塊中的strptime函數,例子如下:
執行結果如下:
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=9, tm_hour=21, tm_min=9, tm_sec=30, tm_wday=0, tm_yday=130, tm_isdst=-1)
2、函數說明:
第一個參數是要轉換成日期格式的字符串,第二個參數是字符串的格式,下面函數可以看一下。
python中,怎麼把字符串轉換為日期格式
python中要把字符串轉換成日期格式需要使用time模塊中的strptime函數,例子如下:
import time
t = time.strptime(‘2016-05-09 21:09:30’, ‘%y-%m-%d %h:%m:%s’)
print(t)執行結果如下:
time.struct_time(tm_year=2016,
tm_mon=5,
tm_mday=9,
tm_hour=21,
tm_min=9,
tm_sec=30,
tm_wday=0,
tm_yday=130,
tm_isdst=-1)
函數說明:
第一個參數是要轉換成日期格式的字符串,第二個參數是字符串的格式
函數官方文檔如下:
help on built-in function strptime in module time:
strptime(…)
strptime(string, format) – struct_time
parse a string to a time tuple according to a format specification.
see the library reference manual for formatting codes (same as
strftime()).
commonly used format codes:
%y year with century as a decimal number.
%m month as a decimal number [01,12].
%d day of the month as a decimal number [01,31].
%h hour (24-hour clock) as a decimal number [00,23].
%m minute as a decimal number [00,59].
%s second as a decimal number [00,61].
%z time zone offset from utc.
%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.
%i hour (12-hour clock) as a decimal number [01,12].
%p locale’s equivalent of either am or pm.
other codes may be available on your platform. see documentation for the c library strftime function.
python中如何把datetime.datetime轉換成datetime.time?
用Python實現字符串和日期相互轉換的方法,具體如下:這裡用的分別是time和datetime函數來處理 importtime,datetime //日期轉化為字符串 #datetostr //輸出時間 printtime.strftime(“%Y-%m-%d%X”,time.localtime()) #strtodate //字符串轉化為日期 t=time.strptime(“2016-12-05″,”%Y-%m-%d”) y,m,d=t[0:3] //輸出時間 printdatetime.datetime(y,m,d)
新手求教:python 時間格式轉換
時間格式轉換分為兩種,時間轉換為字符串和字符串轉換為時間,具體代碼例子如下:
1 import datetime
2 import time
3 # 日期轉換為字符串,使用strftime()函數
4 # time.strftime(format[, t])
5
6 print datetime.datetime.now()
7 print datetime.datetime.now().strftime(“%Y-%m-%d
%H:%M:%S”)
8 print datetime.datetime.now().strftime(“%b
%d %Y %H:%M:%S”)
9 print datetime.datetime.now().strftime(“%c
%d %Y %H:%M:%S”)
10 # 字符串轉換為日期,使用strptime()函數
11 t = (2009, 2, 17, 8, 3, 38, 1, 48, 0)
12 t = time.mktime(t)
13 print time.strftime(“%b %d %Y %H:%M:%S”,time.gmtime(t))
14 print time.strftime(“%Y-%m-%d %H:%M:%S”,time.gmtime(t))
註:格式字符說明:
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
當前時區的名稱
%%
%號本身
python的日期類型轉換
你可以利用 time 模塊里的 strptime()和 strftime()。
strptime()根據你指定的格式控制字符串解讀日期,
而 strftime()則根據你指定的格式控制字符串輸出日期。
比如,把 「12-Jan-06 10:06」 格式轉換成 「2006-01-12 10:06:00」 格式:
from time import strptime, strftime
myDate = ’12-Jan-06 10:06′
parsed = strptime( myDate, ‘%d-%b-%y %H:%M’ )
converted = strftime( ‘%Y-%m-%d %H:%M:00’, parsed )
converted
‘2006-01-12 10:06:00’
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248378.html