本文實例為大家分享了python實現簡單日期工具類的具體代碼,供大家參考,具體內容如下
import datetime
import time
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
TIME_FORMAT = "%H:%M:%S"
#當前毫秒數
def curMilis():
return int(time.time() * 1000)
#當前秒數
def curSeconds():
return int(time.time())
#當前日期 格式%Y-%m-%d %H:%M:%S
def curDatetime():
return datetime.datetime.strftime(datetime.datetime.now(),DATETIME_FORMAT)
#當前日期 格式%Y-%m-%d
def curDate():
return datetime.date.today()
#當前時間 格式%Y-%m-%d
def curTime():
return time.strftime(TIME_FORMAT)
#秒轉日期
def secondsToDatetime(seconds):
return time.strftime(DATETIME_FORMAT,time.localtime(seconds))
#毫秒轉日期
def milisToDatetime(milix):
return time.strftime(DATETIME_FORMAT,time.localtime(milix//1000))
#日期轉毫秒
def datetimeToMilis(datetimestr):
strf = time.strptime(datetimestr,DATETIME_FORMAT)
return int(time.mktime(strf)) * 1000
#日期轉秒
def datetimeToSeconds(datetimestr):
strf = time.strptime(datetimestr,DATETIME_FORMAT)
return int(time.mktime(strf))
#當前年
def curYear():
return datetime.datetime.now().year
#當前月
def curMonth():
return datetime.datetime.now().month
#當前日
def curDay():
return datetime.datetime.now().day
#當前時
def curHour():
return datetime.datetime.now().hour
#當前分
def curMinute():
return datetime.datetime.now().minute
#當前秒
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("當前秒:",secondStamp)
milisStamp = curMilis()
print("當前毫秒:",milisStamp)
curdTime = curDatetime()
print("當前時間:",curdTime)
curDate = curDate()
print("當前日期:",curDate)
curT = curTime()
print("當前時刻:",curT)
stdtime = secondsToDatetime(secondStamp)
print("秒轉時間:",stdtime)
mtdtime = milisToDatetime(milisStamp)
print("毫秒轉時間:",mtdtime)
dtimetm = datetimeToMilis(mtdtime)
print("時間轉毫秒:",dtimetm)
dtimets = datetimeToSeconds(mtdtime)
print("時間轉秒:",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)
輸出結果如下:
當前秒: 1518341913 當前毫秒: 1518341913403 當前時間: 2018-02-11 17:38:33 當前日期: 2018-02-11 當前時刻: 17:38:33 秒轉時間: 2018-02-11 17:38:33 毫秒轉時間: 2018-02-11 17:38:33 時間轉毫秒: 1518341913000 時間轉秒: 1518341913 年: 2018 月: 2 日: 11 時: 17 分: 38 秒: 33 星期: 6 [Finished in 0.2s]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。