有這么一個(gè)需求杏瞻,是要求獲取未來或過去時(shí)間的日期。
如前5天的日期或當(dāng)前日期伏社,輸出日期以給定的分隔進(jìn)行輸出每辟。
代碼如下:
import time,os,datetime
def timestamp():
timestamp = time.strftime("%Y%m%d-%H%M%S")
print timestamp
def getDateConver(conv='-',day=-1):
if conv == '.' or conv == '-':
nowTime = datetime.datetime.now()
if type(day) is int:
yesTime = nowTime + datetime.timedelta(days=day)
dateFmt = '%Y'+ conv +'%m'+ conv +'%d'
yesTime = yesTime.strftime(dateFmt)
nowTime = nowTime.strftime(dateFmt)
return yesTime,nowTime
else:
print 'error, days is only int tpye.'
else:
print 'Date format error. only "." or "-" .'
if __name__ == "__main__":
#timestamp()
print getDateConver('.',-1)
打印示例:
[root@ip~]# python timetest.py
('2018.05.29', '2018.05.30') #打印的是歷史時(shí)間和當(dāng)前今天時(shí)間