python获取一个月之前日期_python获取当前日期前后N天或N月的日期

#-*- coding: utf-8 -*-

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

from time importstrftime, localtimefrom datetime importtimedelta, dateimportcalendar

year= strftime("%Y",localtime())

mon= strftime("%m",localtime())

day= strftime("%d",localtime())

hour= strftime("%H",localtime())

min= strftime("%M",localtime())

sec= strftime("%S",localtime())deftoday():'''''

get today,date format="YYYY-MM-DD"'''''

returndate.today()deftodaystr():'''get date string, date format="YYYYMMDD"'''

return year+mon+daydefdatetime():'''''

get datetime,format="YYYY-MM-DD HH:MM:SS"'''

return strftime("%Y-%m-%d %H:%M:%S",localtime())defdatetimestr():'''''

get datetime string

date format="YYYYMMDDHHMMSS"'''

return year+mon+day+hour+min+secdef get_day_of_day(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)defget_days_of_month(year,mon):'''''

get days of month'''

return calendar.monthrange(year, mon)[1]defget_firstday_of_month(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 inarr)defget_lastday_of_month(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 inarr)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 inarr)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 ingetyearandmonth(n))def getyearandmonth(n=0):'''''

get the year,month,days from today

befor or after n months'''thisyear=int(year)

thismon=int(mon)

totalmon= thismon+nif(n>=0):if(totalmon<=12):

days=str(get_days_of_month(thisyear,totalmon))

totalmon=addzero(totalmon)return(year,totalmon,days)else:

i= totalmon/12j= totalmon%12

if(j==0):

i-=1j=12thisyear+=i

days=str(get_days_of_month(thisyear,j))

j=addzero(j)return(str(thisyear),str(j),days)else:if((totalmon>0) and (totalmon<12)):

days=str(get_days_of_month(thisyear,totalmon))

totalmon=addzero(totalmon)return(year,totalmon,days)else:

i= totalmon/12j= totalmon%12

if(j==0):

i-=1j=12thisyear+=i

days=str(get_days_of_month(thisyear,j))

j=addzero(j)return(str(thisyear),str(j),days)defaddzero(n):'''''

add 0 before 0-9

return 01-09'''nabs=abs(int(n))if(nabs<10):return "0"+str(nabs)else:returnnabsdef get_today_month(n=0):'''''

获取当前日期前后N月的日期

if n>0, 获取当前日期前N月的日期

if n<0, 获取当前日期后N月的日期

date format = "YYYY-MM-DD"'''(y,m,d)=getyearandmonth(n)

arr=(y,m,d)if(int(day)

arr=(y,m,day)return "-".join("%s" %i for i inarr)if __name__=="__main__":printtoday()printtodaystr()printdatetime()printdatetimestr()print get_day_of_day(20)print get_day_of_day(-3)print get_today_month(-3)print get_today_month(3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值