python實(shí)現(xiàn)簡(jiǎn)單日期工具類
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎ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)前時(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)
輸出結(jié)果如下:
當(dāng)前秒: 1518341913 當(dāng)前毫秒: 1518341913403 當(dāng)前時(shí)間: 2018-02-11 17:38:33 當(dāng)前日期: 2018-02-11 當(dāng)前時(shí)刻: 17:38:33 秒轉(zhuǎn)時(shí)間: 2018-02-11 17:38:33 毫秒轉(zhuǎn)時(shí)間: 2018-02-11 17:38:33 時(shí)間轉(zhuǎn)毫秒: 1518341913000 時(shí)間轉(zhuǎn)秒: 1518341913 年: 2018 月: 2 日: 11 時(shí): 17 分: 38 秒: 33 星期: 6 [Finished in 0.2s]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python新手必讀bytearray對(duì)象使用技巧掌握
Python中的bytearray是一個(gè)可變序列,通常用于存儲(chǔ)二進(jìn)制數(shù)據(jù),它允許在不創(chuàng)建新的對(duì)象的情況下就地修改數(shù)據(jù),非常適用于處理字節(jié)數(shù)據(jù),本文將深入學(xué)習(xí)bytearray對(duì)象的使用,包括創(chuàng)建、修改、切片和常見(jiàn)應(yīng)用場(chǎng)景2023-12-12django 解決model中類寫(xiě)不到數(shù)據(jù)庫(kù)中,數(shù)據(jù)庫(kù)無(wú)此字段的問(wèn)題
這篇文章主要介紹了django 解決model中類寫(xiě)不到數(shù)據(jù)庫(kù)中,數(shù)據(jù)庫(kù)無(wú)此字段的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想過(guò)來(lái)看看吧2020-05-05Python竟能畫(huà)這么漂亮的花,帥呆了(代碼分享)
這篇文章主要介紹了用Python作圖的一個(gè)簡(jiǎn)單實(shí)例,通過(guò)turtle模塊實(shí)現(xiàn)作圖,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Python使用Beets模塊實(shí)現(xiàn)自動(dòng)整理音樂(lè)庫(kù)
Beets是一個(gè)功能強(qiáng)大的Python庫(kù),用于處理音樂(lè)文件的元數(shù)據(jù),在本文中,我們將探討beets模塊的常見(jiàn)使用方法,感興趣的可以跟隨小編一起學(xué)習(xí)一下2024-03-03Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能
最近在自己學(xué)習(xí)Python爬蟲(chóng),學(xué)到了用Python發(fā)送郵件,覺(jué)得這個(gè)可能以后比較實(shí)用。所以這篇文章主要給大家介紹了如何通過(guò)Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能的相關(guān)資料,需要的朋友可以參考下2021-05-05python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析
今天小編就為大家分享一篇python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02