python擷取當前日期前後N天或N月的日期

來源:互聯網
上載者:User

來源地址:http://snipperize.todayclose.com/snippet/py/python%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%A5%E6%9C%9F%E5%89%8D%E5%90%8EN%E5%A4%A9%E6%88%96N%E6%9C%88%E7%9A%84%E6%97%A5%E6%9C%9F--97049/

#!/usr/bin/python'''Filename: "utildate.py"author:   "zhangsong"date  :   "2009-03-24"version:  "1.00"'''from time import strftime, localtimefrom datetime import timedelta, dateimport calendar year = strftime("%Y",localtime())mon  = strftime("%m",localtime())day  = strftime("%d",localtime())hour = strftime("%H",localtime())min  = strftime("%M",localtime())sec  = strftime("%S",localtime())  def today():    '''    get today,date format="YYYY-MM-DD"    '''    return date.today() def todaystr():    '''    get date string    date format="YYYYMMDD"    '''    return year+mon+day def datetime():    '''    get datetime,format="YYYY-MM-DD HH:MM:SS"    '''    return strftime("%Y-%m-%d %H:%M:%S",localtime()) def datetimestr():    '''    get datetime string    date format="YYYYMMDDHHMMSS"    '''    return year+mon+day+hour+min+sec def getdayofday(n=0):    '''    if n>=0,date is larger than today    if n<0,date is less than today    date format = "YYYY-MM-DD"    '''    if(n<0):        n = abs(n)        return date.today()-timedelta(days=n)    else:        return date.today()+timedelta(days=n) def getdaysofmonth(year,mon):    '''    get days of month    '''    return calendar.monthrange(year, mon)[1] def getfirstdayofmonth(year,mon):    '''    get the first day of month    date format = "YYYY-MM-DD"    '''    days="01"    if(int(mon)<10):        mon = "0"+str(int(mon))    arr = (year,mon,days)    return "-".join("%s" %i for i in arr) def getlastdayofmonth(year,mon):    '''    get the last day of month    date format = "YYYY-MM-DD"    '''    days=calendar.monthrange(year, mon)[1]    mon = addzero(mon)    arr = (year,mon,days)    return "-".join("%s" %i for i in arr) def get_firstday_month(n=0):    '''    get the first day of month from today    n is how many months    '''    (y,m,d) = getyearandmonth(n)    d = "01"    arr = (y,m,d)    return "-".join("%s" %i for i in arr) def get_lastday_month(n=0):    '''    get the last day of month from today    n is how many months    '''    return "-".join("%s" %i for i in getyearandmonth(n))  def get_today_month(n=0):    '''    get last or next month's today    n is how many months    date format = "YYYY-MM-DD"    '''    (y,m,d) = getyearandmonth(n)    arr=(y,m,d)    if(int(day)<int(d)):        arr = (y,m,day)    return "-".join("%s" %i for i in arr) def getyearandmonth(n=0):    '''    get the year,month,days from today    befor or after n months    '''    thisyear = int(year)    thismon = int(mon)    totalmon = thismon+n    if(n>=0):        if(totalmon<=12):            days = str(getdaysofmonth(thisyear,totalmon))            totalmon = addzero(totalmon)            return (year,totalmon,days)        else:            i = totalmon/12            j = totalmon%12            if(j==0):                i-=1                j=12            thisyear += i            days = str(getdaysofmonth(thisyear,j))            j = addzero(j)            return (str(thisyear),str(j),days)    else:        if((totalmon>0) and (totalmon<12)):            days = str(getdaysofmonth(thisyear,totalmon))            totalmon = addzero(totalmon)            return (year,totalmon,days)        else:            i = totalmon/12            j = totalmon%12            if(j==0):                i-=1                j=12            thisyear +=i            days = str(getdaysofmonth(thisyear,j))            j = addzero(j)            return (str(thisyear),str(j),days) def addzero(n):    '''    add 0 before 0-9    return 01-09    '''    nabs = abs(int(n))    if(nabs<10):        return "0"+str(nabs)    else:        return nabs #print today()#print addzero(10)print get_today_month(-1)print get_lastday_month(3)print get_firstday_month(3)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.