為了要抓高公局的資料,時間格式一直弄不出來,最後感謝強者我同事協助^_^
from datetime import timedelta
import datetime
Start = datetime.datetime(2014,1,1,1,1)
End = datetime.datetime(2014,1,2,0,0)
IntervalTime_minutes = 5
print datetime.timedelta(minutes = IntervalTime_minutes).strftime("%H%M%S")
TimeCount = Start
while TimeCount < End:
TimeCount = TimeCount + datetime.timedelta(minutes = IntervalTime_minutes)
print TimeCount.strftime("%Y%m%d_%H%M%S")
參考網頁:( google > Python時間加減 )
- 設定 someday = datetime.date(2013,10,28)
- datetime.timedelta(days = 1) 用於計算 datetime 時間增減(本例:以1天為單位)
- 可利用 strftime("%Y%m%d) datetime 控制輸出格式
vi test.py
import time
import datetime
someday = datetime.date(2014,9,20)
while (someday <= datetime.date.today()) :
# print out with date format : YYYYMMDD, example : 20140920
print str(someday.strftime("%Y%m%d"))
someday += datetime.timedelta(days = 1)
print "================="
someday = datetime.date.today()
while (someday >= datetime.date(2014,9,15)) :
# print out with date format : YYYYMMDD, example : 2014-09-20
print str(someday.strftime("%Y-%m-%d"))
# minus, timedelta(days = 1)
someday -= datetime.timedelta(days = 1)
print "================="
someday = datetime.date.today()
while (someday >= datetime.date(2014,9,15)) :
# print out with date format : YYYYMMDD, example : 2014/09/20
print str(someday.strftime("%Y/%m/%d"))
# add, timedelta(days = -1)
someday += datetime.timedelta(days = -1)
執行結果:
$ python test.py 20140920 20140921 20140922 ================= 2014-09-22 2014-09-21 2014-09-20 2014-09-19 2014-09-18 2014-09-17 2014-09-16 2014-09-15 ================= 2014/09/22 2014/09/21 2014/09/20 2014/09/19 2014/09/18 2014/09/17 2014/09/16 2014/09/15
延伸閱讀:datetime - Basic date and time types (python.org)
沒有留言:
張貼留言