您好,登錄后才能下訂單哦!
本文實例為大家分享了python實現(xiàn)簡單日期工具類的具體代碼,供大家參考,具體內(nèi)容如下
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)前時間 格式%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)前時 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() #幾天前的時間 def nowDaysAgo(days): daysAgoTime = datetime.datetime.now() - datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #幾天后的時間 def nowDaysAfter(days): daysAgoTime = datetime.datetime.now() + datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #某個日期幾天前的時間 def dtimeDaysAgo(dtimestr,days): daysAgoTime = datetime.datetime.strptime(dtimestr,DATETIME_FORMAT) - datetime.timedelta(days = days) return time.strftime(DATETIME_FORMAT,daysAgoTime.timetuple()) #某個日期幾天前的時間 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)前時間:",curdTime) curDate = curDate() print("當(dāng)前日期:",curDate) curT = curTime() print("當(dāng)前時刻:",curT) stdtime = secondsToDatetime(secondStamp) print("秒轉(zhuǎn)時間:",stdtime) mtdtime = milisToDatetime(milisStamp) print("毫秒轉(zhuǎn)時間:",mtdtime) dtimetm = datetimeToMilis(mtdtime) print("時間轉(zhuǎn)毫秒:",dtimetm) dtimets = datetimeToSeconds(mtdtime) print("時間轉(zhuǎn)秒:",dtimets) year = curYear() print("年:",year) month = curMonth() print("月:",month) day = curDay() print("日:",day) hour = curHour() print("時:",hour) minute = curMinute() print("分:",minute) second = curSecond() print("秒:",second) week = curWeek() print("星期:",week)
輸出結(jié)果如下:
當(dāng)前秒: 1518341913 當(dāng)前毫秒: 1518341913403 當(dāng)前時間: 2018-02-11 17:38:33 當(dāng)前日期: 2018-02-11 當(dāng)前時刻: 17:38:33 秒轉(zhuǎn)時間: 2018-02-11 17:38:33 毫秒轉(zhuǎn)時間: 2018-02-11 17:38:33 時間轉(zhuǎn)毫秒: 1518341913000 時間轉(zhuǎn)秒: 1518341913 年: 2018 月: 2 日: 11 時: 17 分: 38 秒: 33 星期: 6 [Finished in 0.2s]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。