這篇文章將為大家詳細(xì)講解有關(guān)python中datetime的使用方法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、seo優(yōu)化排名、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都火鍋店設(shè)計(jì)等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。具體代碼如下所示:
# 時(shí)間的替換 import time,datetime today = datetime.datetime.today() print('當(dāng)前時(shí)間:',today) print('當(dāng)前時(shí)間轉(zhuǎn)換成整h整m整s:',today.replace(minute=0, second=0)) # 時(shí)間的加減 res1 = today + datetime.timedelta(days=1,minutes=60) print('增加的時(shí)間為res:',res1) res0 = datetime.datetime.now() print('未格式化時(shí)間res0:',res0) # 將時(shí)間轉(zhuǎn)字符串格式化 res2 = datetime.datetime.now().strftime("%Y/%m/%d") print('格式化后的時(shí)間res:',res2) # 字符串轉(zhuǎn)時(shí)間 res3 = datetime.datetime.strptime('2018/10/11', "%Y/%m/%d") print('字符串轉(zhuǎn)特定的時(shí)間res:',res3) # 轉(zhuǎn)成時(shí)間元組 print('轉(zhuǎn)成時(shí)間元組:',today.timetuple()) #日期0000時(shí)間格式轉(zhuǎn)換為普通時(shí)間格式 str_time ='2018-12-14 00:00:00' start_date = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S") print('start_date:',start_date) print('---------第二部分----------') from datetime import datetime from datetime import timedelta # 1) 獲取當(dāng)前日期和時(shí)間 today = datetime.today() # 返回當(dāng)前時(shí)間時(shí)分秒都為0 print('當(dāng)前today:',today) today1 = datetime.now() # 返回當(dāng)前日期和時(shí)間 # now.hour # 時(shí) # now.minute # 分 # now.isoweekday()# 返回的1-7代表周一--周日; # now.weekday()# 返回的0-6代表周一--到周日 # 而標(biāo)準(zhǔn)格式種%w 1-6表示周一--周六,0代表周日 print('today1', today1 ) today2 = datetime.utcnow() # 返回當(dāng)前東八區(qū)時(shí)間就是比當(dāng)時(shí)時(shí)間少8個(gè)小時(shí) print('today2:',today2) # 2) 獲取指定日期和時(shí)間,加減計(jì)算 time= datetime(2019, 5, 12, 12, 13, 14) d= time + timedelta(weeks=0, days=0, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, ) #依次為 "周" "天", "時(shí)","分","秒","毫秒","微秒" print('time:',time) print('d:',d) time1= "2019-5-12 12:13:14" # 字符串 日期 d1 = datetime.strptime(str(time1),'%Y-%m-%d %H:%M:%S') plus= d1 + timedelta(days=1) # 加 minus = d1 - timedelta(days=1) # 減 print('d1:',d1) print('plus:',plus) print('minus:',minus) time2= 20190512121314 d2 = datetime.strptime(str(time2),'%Y%m%d%H%M%S') delta = d2 + timedelta(days=1) print('time2:',time2) print('d2:',d2 ) print('delta:',delta) # 3) 日期datetime-timestamp 時(shí)間戳相互轉(zhuǎn) now_stamp = time.timestamp() print('指定時(shí)間對應(yīng)時(shí)間戳 :', now_stamp) print('對應(yīng)本地時(shí)間 :', datetime.fromtimestamp(now_stamp )) print('UTC標(biāo)準(zhǔn)時(shí)間 :', datetime.utcfromtimestamp(now_stamp )) print('本周的第幾天:', datetime.fromtimestamp(now_stamp ).weekday()) # 4) datetime 時(shí)間 轉(zhuǎn)換為str字符串 now = datetime.now() print('當(dāng)前時(shí)間的的:', now) print('轉(zhuǎn)換為str字符串:',now.strftime('%Y%m%d%H%M%S')) print('--------第三部分-------------') import datetime from datetime import timedelta now = datetime.datetime.now() print('now:',now) # 今天 today = now print('today:',today) # 昨天 yesterday = now - timedelta(days=1) print('yesterday:',yesterday) # 明天 tomorrow = now + timedelta(days=1) print('tomorrow:',tomorrow) # 當(dāng)前季度 now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1 print('now_quarter:',now_quarter) # 本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6 - now.weekday()) print('this_week_start:',this_week_start) print('this_week_end:',this_week_end) # 上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday() + 7) last_week_end = now - timedelta(days=now.weekday() + 1) print('last_week_start:',last_week_start) print('last_week_end:',last_week_end) # 本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) month = now.month + 1 if month == 13: month = 1 year = now.year + 1 this_month_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) print('this_month_end:',this_month_end ) else: this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) print('this_month_end:',this_month_end ) # 上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) print('last_month_end:',last_month_end) print('last_month_start:',last_month_start) # 本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1) print('this_quarter_start:',this_quarter_start) print('this_quarter_end:',this_quarter_end) # 上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) print('last_quarter_end:',last_quarter_end) print('last_quarter_start:',last_quarter_start) # 本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) print('this_year_start:',this_year_start) print('this_year_end:',this_year_end) # 去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1) last_year_start = datetime.datetime(last_year_end.year, 1, 1) print('last_year_end:',last_year_end) print('last_year_start:',last_year_start)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。