新聞中心
python中如何把datetime.datetime轉(zhuǎn)換成datetime.time?
用Python實現(xiàn)字符串和日期相互轉(zhuǎn)換的方法,具體如下:這里用的分別是time和datetime函數(shù)來處理 importtime,datetime //日期轉(zhuǎn)化為字符串 #datetostr //輸出時間 printtime.strftime("%Y-%m-%d%X",time.localtime()) #strtodate //字符串轉(zhuǎn)化為日期 t=time.strptime("2016-12-05","%Y-%m-%d") y,m,d=t[0:3] //輸出時間 printdatetime.datetime(y,m,d)

創(chuàng)新互聯(lián)公司是專業(yè)的長安網(wǎng)站建設(shè)公司,長安接單;提供成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行長安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
[python]統(tǒng)一轉(zhuǎn)換日期格式dateutil.parser.parse
背景:
我有很多很多的日志數(shù)據(jù),每個日志里面都有日期字符串,我需要將其轉(zhuǎn)換為datetime格式。
問題是,這些日志里的字符串格式五花八門,有2017-05-25T05:27:30.313292255Z,有2016-07-01T00:00:00以及其他各種我還沒有看到的格式。
開始我寫了一長串的if else來判斷格式,但是總有我漏掉的。
最后上網(wǎng)一查,發(fā)現(xiàn)dateutil.parser.parse??梢圆挥梦覀冎付ǜ袷?,直接將字符串轉(zhuǎn)換為datetime格式。
注:我試了下"19/May/2017:04:10:06 +0000" 居然失敗了- -!那可能這個函數(shù)只認識數(shù)字不認得字母吧。
python中,怎么把字符串轉(zhuǎn)換為日期格式
python中要把字符串轉(zhuǎn)換成日期格式需要使用time模塊中的strptime函數(shù),例子如下:
import time
t = time.strptime('2016-05-09 21:09:30', '%y-%m-%d %h:%m:%s')
print(t)執(zhí)行結(jié)果如下:
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)
函數(shù)說明:
第一個參數(shù)是要轉(zhuǎn)換成日期格式的字符串,第二個參數(shù)是字符串的格式
函數(shù)官方文檔如下:
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 時間格式轉(zhuǎn)換
時間格式轉(zhuǎn)換分為兩種,時間轉(zhuǎn)換為字符串和字符串轉(zhuǎn)換為時間,具體代碼例子如下:
1?import?datetime
2?import?time
3?#?日期轉(zhuǎn)換為字符串,使用strftime()函數(shù)
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?#?字符串轉(zhuǎn)換為日期,使用strptime()函數(shù)
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
兩位數(shù)的年份表示(00-99)
%Y
四位數(shù)的年份表示(000-9999)
%m
月份(01-12)
%d
月內(nèi)中的一天(0-31)
%H
24小時制小時數(shù)(0-23)
%I
12小時制小時數(shù)(01-12)
%M
分鐘數(shù)(00=59)
%S
秒(00-59)
%a
本地簡化星期名稱
%A
本地完整星期名稱
%b
本地簡化的月份名稱
%B
本地完整的月份名稱
%c
本地相應(yīng)的日期表示和時間表示
%j
年內(nèi)的一天(001-366)
%p
本地A.M.或P.M.的等價符
%U
一年中的星期數(shù)(00-53)星期天為星期的開始
%w
星期(0-6),星期天為星期的開始
%W
一年中的星期數(shù)(00-53)星期一為星期的開始
%x
本地相應(yīng)的日期表示
%X
本地相應(yīng)的時間表示
%Z
當(dāng)前時區(qū)的名稱
%%
%號本身
文章標題:python日期轉(zhuǎn)換函數(shù) python日期轉(zhuǎn)換為天數(shù)
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/dodspoj.html


咨詢
建站咨詢
