zoukankan      html  css  js  c++  java
  • 【python】python获取当前日期前后N天或N月的日期

      1 # -*- coding: utf-8 -*-
      2 
      3 '''获取当前日期前后N天或N月的日期'''
      4 
      5 from time import strftime, localtime
      6 from datetime import timedelta, date
      7 import calendar
      8 
      9 year = strftime("%Y",localtime())
     10 mon  = strftime("%m",localtime())
     11 day  = strftime("%d",localtime())
     12 hour = strftime("%H",localtime())
     13 min  = strftime("%M",localtime())
     14 sec  = strftime("%S",localtime())
     15 
     16 def today():
     17     '''''
     18     get today,date format="YYYY-MM-DD"
     19     '''''
     20     return date.today()
     21 
     22 def todaystr():
     23     '''
     24     get date string, date format="YYYYMMDD"
     25     '''
     26     return year+mon+day
     27 
     28 def datetime():
     29     '''''
     30     get datetime,format="YYYY-MM-DD HH:MM:SS"
     31     '''
     32     return strftime("%Y-%m-%d %H:%M:%S",localtime())
     33 
     34 def datetimestr():
     35     '''''
     36     get datetime string
     37     date format="YYYYMMDDHHMMSS"
     38     '''
     39     return year+mon+day+hour+min+sec
     40 
     41 def get_day_of_day(n=0):
     42     '''''
     43     if n>=0,date is larger than today
     44     if n<0,date is less than today
     45     date format = "YYYY-MM-DD"
     46     '''
     47     if(n<0):
     48         n = abs(n)
     49         return date.today()-timedelta(days=n)
     50     else:
     51         return date.today()+timedelta(days=n)
     52 
     53 def get_days_of_month(year,mon): 
     54     ''''' 
     55     get days of month 
     56     ''' 
     57     return calendar.monthrange(year, mon)[1] 
     58   
     59 def get_firstday_of_month(year,mon): 
     60     ''''' 
     61     get the first day of month 
     62     date format = "YYYY-MM-DD" 
     63     ''' 
     64     days="01" 
     65     if(int(mon)<10): 
     66         mon = "0"+str(int(mon)) 
     67     arr = (year,mon,days) 
     68     return "-".join("%s" %i for i in arr) 
     69   
     70 def get_lastday_of_month(year,mon): 
     71     ''''' 
     72     get the last day of month 
     73     date format = "YYYY-MM-DD" 
     74     ''' 
     75     days=calendar.monthrange(year, mon)[1] 
     76     mon = addzero(mon) 
     77     arr = (year,mon,days) 
     78     return "-".join("%s" %i for i in arr) 
     79   
     80 def get_firstday_month(n=0): 
     81     ''''' 
     82     get the first day of month from today 
     83     n is how many months 
     84     ''' 
     85     (y,m,d) = getyearandmonth(n) 
     86     d = "01" 
     87     arr = (y,m,d) 
     88     return "-".join("%s" %i for i in arr) 
     89   
     90 def get_lastday_month(n=0): 
     91     ''''' 
     92     get the last day of month from today 
     93     n is how many months 
     94     ''' 
     95     return "-".join("%s" %i for i in getyearandmonth(n)) 
     96  
     97 def getyearandmonth(n=0): 
     98     ''''' 
     99     get the year,month,days from today 
    100     befor or after n months 
    101     ''' 
    102     thisyear = int(year) 
    103     thismon = int(mon) 
    104     totalmon = thismon+n 
    105     if(n>=0): 
    106         if(totalmon<=12): 
    107             days = str(get_days_of_month(thisyear,totalmon)) 
    108             totalmon = addzero(totalmon) 
    109             return (year,totalmon,days) 
    110         else: 
    111             i = totalmon/12 
    112             j = totalmon%12 
    113             if(j==0): 
    114                 i-=1 
    115                 j=12 
    116             thisyear += i 
    117             days = str(get_days_of_month(thisyear,j)) 
    118             j = addzero(j) 
    119             return (str(thisyear),str(j),days) 
    120     else: 
    121         if((totalmon>0) and (totalmon<12)): 
    122             days = str(get_days_of_month(thisyear,totalmon)) 
    123             totalmon = addzero(totalmon) 
    124             return (year,totalmon,days) 
    125         else: 
    126             i = totalmon/12 
    127             j = totalmon%12 
    128             if(j==0): 
    129                 i-=1 
    130                 j=12 
    131             thisyear +=i 
    132             days = str(get_days_of_month(thisyear,j)) 
    133             j = addzero(j) 
    134             return (str(thisyear),str(j),days) 
    135   
    136 def addzero(n): 
    137     ''''' 
    138     add 0 before 0-9 
    139     return 01-09 
    140     ''' 
    141     nabs = abs(int(n)) 
    142     if(nabs<10): 
    143         return "0"+str(nabs) 
    144     else: 
    145         return nabs 
    146 
    147 def get_today_month(n=0): 
    148     ''''' 
    149     获取当前日期前后N月的日期
    150     if n>0, 获取当前日期前N月的日期
    151     if n<0, 获取当前日期后N月的日期
    152     date format = "YYYY-MM-DD" 
    153     ''' 
    154     (y,m,d) = getyearandmonth(n) 
    155     arr=(y,m,d) 
    156     if(int(day)<int(d)): 
    157         arr = (y,m,day) 
    158     return "-".join("%s" %i for i in arr) 
    159   
    160 
    161 if __name__=="__main__":
    162     print today()  
    163     print todaystr()
    164     print datetime()
    165     print datetimestr()
    166     print get_day_of_day(20)
    167     print get_day_of_day(-3)
    168     print get_today_month(-3)
    169     print get_today_month(3)
  • 相关阅读:
    Linux查看进程和已知端口是否启动
    plsql安装
    windows中用批处理文件删除n天前的文件
    阿里云服务器25邮件端口问题
    通过rpm安装crontab
    [RHEL7.1]关闭防火墙及SElinux
    有了 itchat, python 调用微信个人号从未如此简单(新增 py3 支持)
    Linux中tty、pty、pts的概念区别
    不用写代码就能实现深度学习?手把手教你用英伟达 DIGITS 解决图像分类问题
    Hadoop 2.7.3 安装配置及测试
  • 原文地址:https://www.cnblogs.com/yanglang/p/7490832.html
Copyright © 2011-2022 走看看