Python常用的日期時間處理方法樣本

來源:互聯網
上載者:User
#-*- coding: utf-8 -*-import datetime#給定日期向後N天的日期def dateadd_day(days):  d1 = datetime.datetime.now()  d3 = d1 + datetime.timedelta(days)  return d3#昨天def getYesterday():  today = datetime.date.today()  oneday = datetime.timedelta(days=1)  yesterday = today - oneday  return yesterday#今天def getToday():  return datetime.date.today()#擷取給定參數的前幾天的日期,返回一個listdef getDaysBefore(num):  today = datetime.date.today()  oneday = datetime.timedelta(days=1)  li = []  for i in range(0, num):    #今天減一天,一天一天減    today = today - oneday    #把日期轉換成字串    li.append(datetostr(today))  return li#將字串轉換成datetime類型def strtodatetime(datestr, format):  return datetime.datetime.strptime(datestr, format)#時間轉換成字串,格式為2015-02-02def datetostr(date):  return str(date)[0:10]#時間轉換成字串,格式為2015-02-02def datetostr_secod(date):  return str(date)[0:19]#兩個日期相隔多少天,例:2015-2-04和2015-3-1def datediff(beginDate, endDate):  format = "%Y-%m-%d"  bd = strtodatetime(beginDate, format)  ed = strtodatetime(endDate, format)  oneday = datetime.timedelta(days=1)  count = 0  while bd != ed:    ed = ed - oneday    count += 1  return count#兩個日期之間相差的秒def datediff_seconds(beginDate, endDate):  format = "%Y-%m-%d %H:%M:%S"  if " " not in beginDate or ':' not in beginDate:    bformat = "%Y-%m-%d"  else:    bformat = format  if " " not in endDate or ':' not in endDate:    eformat = "%Y-%m-%d"  else:    eformat = format  starttime = strtodatetime(beginDate, bformat)  endtime = strtodatetime(endDate, eformat)  ret = endtime - starttime  return ret.days * 86400 + ret.seconds#擷取兩個時間段的所有時間,返回listdef getDays(beginDate, endDate):  format = "%Y-%m-%d"  begin = strtodatetime(beginDate, format)  oneday = datetime.timedelta(days=1)  num = datediff(beginDate, endDate) + 1  li = []  for i in range(0, num):    li.append(datetostr(begin))    begin = begin + oneday  return li#擷取當前年份 是一個字串def getYear(date=datetime.date.today()):  return str(date)[0:4]#擷取當前月份 是一個字串def getMonth(date=datetime.date.today()):  return str(date)[5:7]#擷取當前天 是一個字串def getDay(date=datetime.date.today()):  return str(date)[8:10]#擷取當前小時 是一個字串def getHour(date=datetime.datetime.now()):  return str(date)[11:13]#擷取當前分鐘 是一個字串def getMinute(date=datetime.datetime.now()):  return str(date)[14:16]#擷取當前秒 是一個字串def getSecond(date=datetime.datetime.now()):  return str(date)[17:19]def getNow():  return datetime.datetime.now()print dateadd_day(10)#2015-02-14 16:41:13.275000print getYesterday()#2015-02-03print getToday()#2015-02-04print getDaysBefore(3)#['2015-02-03', '2015-02-02', '2015-02-01']print datediff('2015-2-01', '2015-10-05')#246print datediff_seconds('2015-02-04', '2015-02-05')#86400print datediff_seconds('2015-02-04 22:00:00', '2015-02-05')#7200print getDays('2015-2-03', '2015-2-05')#['2015-02-03', '2015-02-04', '2015-02-05']print datetostr_secod(getNow())#2015-02-04 16:46:47print str(getYear(dateadd_day(-50))) + '-'\  + getMonth() + '-'\  + getDay() + ' '\  + getHour() + ':'\  + getMinute() + ':'\  + getSecond()#2014-02-04 16:59:04print getNow()#2015-02-04 16:46:47.454000
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.