本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單日期工具類(lèi)的具體代碼,供大家參考,具體內(nèi)容如下
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)海棠免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" TIME_FORMAT = "%H:%M:%S" #當(dāng)前毫秒數(shù) def curMilis(): return int(time.time() * 1000) #當(dāng)前秒數(shù) def curSeconds(): return int(time.time()) #當(dāng)前日期 格式%Y-%m-%d %H:%M:%S def curDatetime(): return datetime.datetime.strftime(datetime.datetime.now(),DATETIME_FORMAT) #當(dāng)前日期 格式%Y-%m-%d def curDate(): return datetime.date.today() #當(dāng)前時(shí)間 格式%Y-%m-%d def curTime(): return time.strftime(TIME_FORMAT) #秒轉(zhuǎn)日期 def secondsToDatetime(seconds): return time.strftime(DATETIME_FORMAT,time.localtime(seconds)) #毫秒轉(zhuǎn)日期 def milisToDatetime(milix): return time.strftime(DATETIME_FORMAT,time.localtime(milix//1000)) #日期轉(zhuǎn)毫秒 def datetimeToMilis(datetimestr): strf = time.strptime(datetimestr,DATETIME_FORMAT) return int(time.mktime(strf)) * 1000 #日期轉(zhuǎn)秒 def datetimeToSeconds(datetimestr): strf = time.strptime(datetimestr,DATETIME_FORMAT) return int(time.mktime(strf)) #當(dāng)前年 def curYear(): return datetime.datetime.now().year #當(dāng)前月 def curMonth(): return datetime.datetime.now().month #當(dāng)前日 def curDay(): return datetime.datetime.now().day #當(dāng)前時(shí) def curHour(): return datetime.datetime.now().hour #當(dāng)前分 def curMinute(): return datetime.datetime.now().minute #當(dāng)前秒 def curSecond(): return datetime.datetime.now().second #星期幾 def curWeek(): return datetime.datetime.now().weekday() #幾天前的時(shí)間 def nowDaysAgo(days): daysAgoTime = datetime.datetime.now() - datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #幾天后的時(shí)間 def nowDaysAfter(days): daysAgoTime = datetime.datetime.now() + datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #某個(gè)日期幾天前的時(shí)間 def dtimeDaysAgo(dtimestr,days): daysAgoTime = datetime.datetime.strptime(dtimestr,DATETIME_FORMAT) - datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #某個(gè)日期幾天前的時(shí)間 def dtimeDaysAfter(dtimestr,days): daysAgoTime = datetime.datetime.strptime(dtimestr,DATETIME_FORMAT) + datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) secondStamp = curSeconds() print("當(dāng)前秒:",secondStamp) milisStamp = curMilis() print("當(dāng)前毫秒:",milisStamp) curdTime = curDatetime() print("當(dāng)前時(shí)間:",curdTime) curDate = curDate() print("當(dāng)前日期:",curDate) curT = curTime() print("當(dāng)前時(shí)刻:",curT) stdtime = secondsToDatetime(secondStamp) print("秒轉(zhuǎn)時(shí)間:",stdtime) mtdtime = milisToDatetime(milisStamp) print("毫秒轉(zhuǎn)時(shí)間:",mtdtime) dtimetm = datetimeToMilis(mtdtime) print("時(shí)間轉(zhuǎn)毫秒:",dtimetm) dtimets = datetimeToSeconds(mtdtime) print("時(shí)間轉(zhuǎn)秒:",dtimets) year = curYear() print("年:",year) month = curMonth() print("月:",month) day = curDay() print("日:",day) hour = curHour() print("時(shí):",hour) minute = curMinute() print("分:",minute) second = curSecond() print("秒:",second) week = curWeek() print("星期:",week)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。