日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Python里面關于日期計算的自帶模塊主要就是time和datetime,兩個模塊提供的側重功能點不盡相同,本文主要是對我進入工作幾年以來所涉及使用到的最頻繁最有效的日期計算功能進行的總結和記錄,分享給每一個pythoner,希望這些日期計算的小工具能夠幫助您提升在工作計算中的效率。

代碼實現如下:

#!usr/bin/env python
#encoding:utf-8
 
 
'''
__Author__:沂水寒城
功能: 日期計算操作記錄大全
'''
 
import time
import datetime
 
def datetime2String(timestamp,format='%Y-%m-%d %H:%M:%S'):
 '''
 把datetime轉成字符串
 '''
 res=timestamp.strftime(format)
 print 'res: ',res
 return res
 
 
def string2Datetime(timestamp,format='%Y-%m-%d %H:%M:%S'):
 '''
 把字符串轉成datetime
 '''
 res=datetime.datetime.strptime(timestamp,format)
 print 'res: ',res
 return res
 
 
def string2Timestamp(timestamp):
 '''
 把字符串轉成時間戳形式
 '''
 res=time.mktime(string2Datetime(timestamp).timetuple())
 print 'res: ',res
 return res
 
 
def timestamp2String(timestamp,format='%Y-%m-%d %H:%M:%S'):
 '''
 把時間戳轉成字符串形式
 '''
 res=time.strftime("%Y-%m-%d-%H", time.localtime(timestamp))
 print 'res: ',res
 return res
 
 
def datetime2Timestamp(one_data):
 '''
 把datetime類型轉為時間戳形式
 '''
 res=time.mktime(one_data.timetuple())
 print 'res: ',res
 return res
 
 
def string2Array(timestr='2018-11-11 11:11:11',format='%Y-%m-%d %H:%M:%S'):
 '''
 將字符串轉化為時間數組對象
 '''
 timeArray=time.strptime(timestr,format)
 print 'timeArray: ',timeArray 
 print 'year: ',timeArray.tm_year
 print 'month: ',timeArray.tm_mon
 print 'day: ',timeArray.tm_mday
 print 'hour: ',timeArray.tm_hour
 print 'minute: ',timeArray.tm_min
 print 'second: ',timeArray.tm_sec
def calTimeDelta(timestamp1='2018-11-16 19:21:22',timestamp2='2018-12-07 10:21:22',format='%Y-%m-%d %H:%M:%S'):
 '''
 計算給定的兩個時間之間的差值
 '''
 T1=datetime.datetime.strptime(timestamp1,format)
 T2=datetime.datetime.strptime(timestamp2,format)
 delta=T2-T1
 day_num=delta.days
 sec_num=delta.seconds
 total_seconds=day_num*86400+sec_num
 print 'dayNum: {0}, secNum: {1}, total_seconds: {2}.'.format(day_num,sec_num,total_seconds)
 return total_seconds
def getBeforeSecond(timestamp,seconds,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 seconds 秒得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(seconds):
 now_time-=datetime.timedelta(seconds=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp 
def getBeforeMinute(timestamp,minutes,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 minutes 分鐘得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(minutes):
 now_time-=datetime.timedelta(minutes=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
 
def getBeforeHour(timestamp,hours,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 hours 個小時得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(hours):
 now_time-=datetime.timedelta(hours=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
 
def getBeforeDay(timestamp,days,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 days 天得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(days):
 now_time-=datetime.timedelta(days=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getBeforeWeek(timestamp,weeks,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 weeks 個星期后得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(weeks):
 now_time-=datetime.timedelta(weeks=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getBeforeMonth(timestamp,months,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 months 個月后得到對應的時間戳
 '''
 from calendar import monthrange
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 year,month,day=[int(one) for one in str(now_time).split(' ')[0].split('-')]
 for i in range(months):
 now_time-=datetime.timedelta(days=monthrange(year,month)[1])
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getBeforeYear(timestamp,years,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,后退 years 年后得到對應的時間戳
 '''
 from calendar import monthrange
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 year,month,day=[int(one) for one in str(now_time).split(' ')[0].split('-')]
 for j in range(years):
 for i in range(12):
 now_time-=datetime.timedelta(days=monthrange(year,month)[1])
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureSecond(timestamp,seconds,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 seconds 秒得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(seconds):
 now_time+=datetime.timedelta(seconds=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureMinute(timestamp,minutes,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 minutes 分鐘得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(minutes):
 now_time+=datetime.timedelta(minutes=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
 
def getFutureHour(timestamp,hours,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 hours 個小時得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(hours):
 now_time+=datetime.timedelta(hours=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureDay(timestamp,days,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 days 天得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(days):
 now_time+=datetime.timedelta(days=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureWeek(timestamp,weeks,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 weeks 個星期后得到對應的時間戳
 '''
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 for i in range(weeks):
 now_time+=datetime.timedelta(weeks=1)
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureMonth(timestamp,months,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 months 個月后得到對應的時間戳
 '''
 from calendar import monthrange
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 year,month,day=[int(one) for one in str(now_time).split(' ')[0].split('-')]
 for i in range(months):
 now_time+=datetime.timedelta(days=monthrange(year,month)[1])
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getFutureYear(timestamp,years,format='%Y-%m-%d %H:%M:%S'):
 '''
 以給定時間戳為基準,前進 years 年后得到對應的時間戳
 '''
 from calendar import monthrange
 now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d %H:%M:%S')
 year,month,day=[int(one) for one in str(now_time).split(' ')[0].split('-')]
 for j in range(years):
 for i in range(12):
 now_time+=datetime.timedelta(days=monthrange(year,month)[1])
 next_timestamp=now_time.strftime('%Y-%m-%d %H:%M:%S')
 print 'next_timestamp: ',next_timestamp
 return next_timestamp
def getNowTimeStamp(format='%Y%m%d'):
 '''
 獲取當前的時間戳
 '''
 now_time=str(datetime.datetime.now().strftime(format))
 nowTime=str(time.strftime(format,time.localtime(time.time())))
 print 'now_time:',now_time
 print 'nowTime:',nowTime
def calDayWeek(one_date):
 '''
 計算指定日期是第幾周
 '''
 year1,month1,day1=[int(one) for one in one_date.split('/')]
 tmp=datetime.date(year1,month1,day1)
 info=list(tmp.isocalendar()) 
 print '{0}是第{1}周周{2}'.format(one_date,info[1],info[-1])
 
 
def calDayAfterWeeksDate(one_date,n_weeks=100):
 '''
 計算指定日期后n_weeks周后是某年某月某日
 '''
 year1,month1,day1=[int(one) for one in one_date.split('/')]
 tmp=datetime.date(year1,month1,day1)
 delta=datetime.timedelta(weeks=n_weeks)
 new_date=(tmp+delta).strftime("%Y-%m-%d %H:%M:%S").split(' ')[0]
 print '{0}過{1}周后日期為:{2}'.format(one_date,n_weeks,new_date)
 
 
def calDayAfterDaysDate(one_date,n_days=100):
 '''
 計算指定日期后n_days天后是某年某月某日
 '''
 year1,month1,day1=[int(one) for one in one_date.split('/')]
 tmp=datetime.date(year1,month1,day1)
 delta=datetime.timedelta(days=n_days)
 new_date=(tmp+delta).strftime("%Y-%m-%d %H:%M:%S").split(' ')[0]
 print '{0}過{1}天后日期為:{2}'.format(one_date,n_days,new_date)
 
if __name__=='__main__':
 #與周相關的計算
 calDayWeek('2015/09/21')
 calDayAfterWeeksDate('2015/09/21',n_weeks=100)
 calDayAfterDaysDate('2015/09/21',n_days=100)
 #計算時間間隔秒數
 calTimeDelta(timestamp1='2018-11-16 19:21:22',timestamp2='2018-12-07 10:21:22',format='%Y-%m-%d %H:%M:%S')
 
 #生成當前時刻的時間戳
 format_list=['%Y%m%d','%Y:%m:%d','%Y-%m-%d','%Y%m%d%H%M%S','%Y-%m-%d %H:%M:%S','%Y/%m/%d/%H:%M:%S']
 for format in format_list:
 getNowTimeStamp(format=format)
 #生成過去間隔指定長度時刻的時間戳
 getBeforeSecond('2018-12-19 11:00:00',40,format='%Y-%m-%d %H:%M:%S')
 getBeforeMinute('2018-12-19 11:00:00',10,format='%Y-%m-%d %H:%M:%S')
 getBeforeHour('2018-12-19 11:00:00',8,format='%Y-%m-%d %H:%M:%S')
 getBeforeDay('2018-12-19 11:00:00',5,format='%Y-%m-%d %H:%M:%S')
 getBeforeWeek('2018-12-19 11:00:00',2,format='%Y-%m-%d %H:%M:%S')
 getBeforeMonth('2018-12-19 11:00:00',3,format='%Y-%m-%d %H:%M:%S')
 getBeforeYear('2018-12-19 11:00:00',10,format='%Y-%m-%d %H:%M:%S')
 #生成未來間隔指定長度時刻的時間戳
 getFutureSecond('2018-12-19 11:00:00',40,format='%Y-%m-%d %H:%M:%S')
 getFutureMinute('2018-12-19 11:00:00',10,format='%Y-%m-%d %H:%M:%S')
 getFutureHour('2018-12-19 11:00:00',8,format='%Y-%m-%d %H:%M:%S')
 getFutureDay('2018-12-19 11:00:00',5,format='%Y-%m-%d %H:%M:%S')
 getFutureWeek('2018-12-19 11:00:00',2,format='%Y-%m-%d %H:%M:%S')
 getFutureMonth('2018-12-19 11:00:00',3,format='%Y-%m-%d %H:%M:%S')
 getFutureYear('2018-12-19 11:00:00',10,format='%Y-%m-%d %H:%M:%S')

簡單對上述代碼測試,輸出結果如下:

2015/09/21是第39周周1
2015/09/21過100周后日期為:2017-08-21
2015/09/21過100天后日期為:2015-12-30
dayNum: 20, secNum: 54000, total_seconds: 1782000.
now_time: 20190801
nowTime: 20190801
now_time: 2019:08:01
nowTime: 2019:08:01
now_time: 2019-08-01
nowTime: 2019-08-01
now_time: 20190801151336
nowTime: 20190801151336
now_time: 2019-08-01 15:13:36
nowTime: 2019-08-01 15:13:36
now_time: 2019/08/01/15:13:36
nowTime: 2019/08/01/15:13:36
next_timestamp: 2018-12-19 10:59:20
next_timestamp: 2018-12-19 10:50:00
next_timestamp: 2018-12-19 03:00:00
next_timestamp: 2018-12-14 11:00:00
next_timestamp: 2018-12-05 11:00:00
next_timestamp: 2018-09-17 11:00:00
next_timestamp: 2008-10-12 11:00:00
next_timestamp: 2018-12-19 11:00:40
next_timestamp: 2018-12-19 11:10:00
next_timestamp: 2018-12-19 19:00:00
next_timestamp: 2018-12-24 11:00:00
next_timestamp: 2019-01-02 11:00:00
next_timestamp: 2019-03-22 11:00:00
next_timestamp: 2029-02-24 11:00:00

個人的經驗累積,需要的可以拿去使用哈,歡迎交流學習!

分享到:
標簽:Python
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定