python获取当前日期前后N天或N月的日期

来源地址: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/

 
  
  1. #!/usr/bin/python 
  2. ''''' 
  3. Filename: "utildate.py" 
  4. author:   "zhangsong" 
  5. date  :   "2009-03-24" 
  6. version:  "1.00" 
  7. ''' 
  8. from time import strftime, localtime 
  9. from datetime import timedelta, date 
  10. import calendar 
  11.   
  12. year = strftime("%Y",localtime()) 
  13. mon  = strftime("%m",localtime()) 
  14. day  = strftime("%d",localtime()) 
  15. hour = strftime("%H",localtime()) 
  16. min  = strftime("%M",localtime()) 
  17. sec  = strftime("%S",localtime()) 
  18.   
  19.   
  20. def today(): 
  21.     ''''' 
  22.     get today,date format="YYYY-MM-DD" 
  23.     ''' 
  24.     return date.today() 
  25.   
  26. def todaystr(): 
  27.     ''''' 
  28.     get date string 
  29.     date format="YYYYMMDD" 
  30.     ''' 
  31.     return year+mon+day 
  32.   
  33. def datetime(): 
  34.     ''''' 
  35.     get datetime,format="YYYY-MM-DD HH:MM:SS" 
  36.     ''' 
  37.     return strftime("%Y-%m-%d %H:%M:%S",localtime()) 
  38.   
  39. def datetimestr(): 
  40.     ''''' 
  41.     get datetime string 
  42.     date format="YYYYMMDDHHMMSS" 
  43.     ''' 
  44.     return year+mon+day+hour+min+sec 
  45.   
  46. def getdayofday(n=0): 
  47.     ''''' 
  48.     if n>=0,date is larger than today 
  49.     if n<0,date is less than today 
  50.     date format = "YYYY-MM-DD" 
  51.     ''' 
  52.     if(n<0): 
  53.         n = abs(n) 
  54.         return date.today()-timedelta(days=n) 
  55.     else
  56.         return date.today()+timedelta(days=n) 
  57.   
  58. def getdaysofmonth(year,mon): 
  59.     ''''' 
  60.     get days of month 
  61.     ''' 
  62.     return calendar.monthrange(year, mon)[1
  63.   
  64. def getfirstdayofmonth(year,mon): 
  65.     ''''' 
  66.     get the first day of month 
  67.     date format = "YYYY-MM-DD" 
  68.     ''' 
  69.     days="01" 
  70.     if(int(mon)<10): 
  71.         mon = "0"+str(int(mon)) 
  72.     arr = (year,mon,days) 
  73.     return "-".join("%s" %i for i in arr) 
  74.   
  75. def getlastdayofmonth(year,mon): 
  76.     ''''' 
  77.     get the last day of month 
  78.     date format = "YYYY-MM-DD" 
  79.     ''' 
  80.     days=calendar.monthrange(year, mon)[1
  81.     mon = addzero(mon) 
  82.     arr = (year,mon,days) 
  83.     return "-".join("%s" %i for i in arr) 
  84.   
  85. def get_firstday_month(n=0): 
  86.     ''''' 
  87.     get the first day of month from today 
  88.     n is how many months 
  89.     ''' 
  90.     (y,m,d) = getyearandmonth(n) 
  91.     d = "01" 
  92.     arr = (y,m,d) 
  93.     return "-".join("%s" %i for i in arr) 
  94.   
  95. def get_lastday_month(n=0): 
  96.     ''''' 
  97.     get the last day of month from today 
  98.     n is how many months 
  99.     ''' 
  100.     return "-".join("%s" %i for i in getyearandmonth(n)) 
  101.    
  102. def get_today_month(n=0): 
  103.     ''''' 
  104.     get last or next month's today 
  105.     n is how many months 
  106.     date format = "YYYY-MM-DD" 
  107.     ''' 
  108.     (y,m,d) = getyearandmonth(n) 
  109.     arr=(y,m,d) 
  110.     if(int(day)<int(d)): 
  111.         arr = (y,m,day) 
  112.     return "-".join("%s" %i for i in arr) 
  113.   
  114. def getyearandmonth(n=0): 
  115.     ''''' 
  116.     get the year,month,days from today 
  117.     befor or after n months 
  118.     ''' 
  119.     thisyear = int(year) 
  120.     thismon = int(mon) 
  121.     totalmon = thismon+n 
  122.     if(n>=0): 
  123.         if(totalmon<=12): 
  124.             days = str(getdaysofmonth(thisyear,totalmon)) 
  125.             totalmon = addzero(totalmon) 
  126.             return (year,totalmon,days) 
  127.         else
  128.             i = totalmon/12 
  129.             j = totalmon%12 
  130.             if(j==0): 
  131.                 i-=1 
  132.                 j=12 
  133.             thisyear += i 
  134.             days = str(getdaysofmonth(thisyear,j)) 
  135.             j = addzero(j) 
  136.             return (str(thisyear),str(j),days) 
  137.     else
  138.         if((totalmon>0and (totalmon<12)): 
  139.             days = str(getdaysofmonth(thisyear,totalmon)) 
  140.             totalmon = addzero(totalmon) 
  141.             return (year,totalmon,days) 
  142.         else
  143.             i = totalmon/12 
  144.             j = totalmon%12 
  145.             if(j==0): 
  146.                 i-=1 
  147.                 j=12 
  148.             thisyear +=i 
  149.             days = str(getdaysofmonth(thisyear,j)) 
  150.             j = addzero(j) 
  151.             return (str(thisyear),str(j),days) 
  152.   
  153. def addzero(n): 
  154.     ''''' 
  155.     add 0 before 0-9 
  156.     return 01-09 
  157.     ''' 
  158.     nabs = abs(int(n)) 
  159.     if(nabs<10): 
  160.         return "0"+str(nabs) 
  161.     else
  162.         return nabs 
  163.   
  164. #print today() 
  165. #print addzero(10) 
  166. print get_today_month(-1
  167. print get_lastday_month(3
  168. print get_firstday_month(3