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

简介:
 
  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

 本文转自阿汐 51CTO博客,原文链接:http://blog.51cto.com/axiii/301210,如需转载请自行联系原作者


相关文章
|
11天前
|
Python
「Python系列」Python 日期和时间
Python 提供了多个内置模块来处理日期和时间,其中最常用的是 `datetime` 模块。这个模块提供了类来操作日期、时间、日期和时间间隔。
25 0
|
1月前
|
Unix 数据处理 Python
Python中日期时间的处理
Python中日期时间的处理
24 0
|
3月前
|
编译器 Python
Python关于日期的记录
Python关于日期的记录
18 0
|
3月前
|
Python
python获取当前日期
python获取当前日期
31 1
|
6月前
|
存储 Linux C语言
Python标准库分享之时间与日期 (time, datetime包)
Python标准库分享之时间与日期 (time, datetime包)
|
1月前
|
安全 Python
Python如何使用datetime模块进行日期和时间的操作
Python如何使用datetime模块进行日期和时间的操作
25 1
|
1月前
|
开发者 Python
Python生成日期和时间
Python生成日期和时间
16 0
|
2月前
|
存储 Python
用Python提取长时间序列遥感文件中缺失文件所对应的日期
【2月更文挑战第1天】本文介绍批量下载大量多时相的遥感影像文件后,基于Python语言与每一景遥感影像文件的文件名,对这些已下载的影像文件加以缺失情况的核对,并自动统计、列出未下载影像所对应的时相的方法~
用Python提取长时间序列遥感文件中缺失文件所对应的日期
|
2月前
|
BI Python
Python获取上个月最后一天的日期
Python获取上个月最后一天的日期
35 0
Python获取上个月最后一天的日期
|
3月前
|
Python
Python 时间日期处理库函数
Python 时间日期处理库函数
55 0
Python 时间日期处理库函数