python获取系统时间月份_python里如何获取当前日期前后N天或N月的日期

#!/usr/bin/python

#_*_ coding:UTF-8_*_

import time

import datetime

import math

import calendar

'''

time.localtime得到的是元祖形式的时间struct_time

time.strftime 得到的是字符串类型的时间

'''

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

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

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

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

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

sec = time.strftime("%S",time.localtime())

def today():

'''

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

'''

return datetime.date.today()

def todaystr():

'''

get date String date format="YYYYMMDD"

'''

return year+mon+day

def datetime1():

'''

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

'''

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

def datetimestr():

'''

get datetime string

date format="YYYYMMDDHHMMSS"

'''

return year+mon+day+hour+min+sec

def 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)

'''

datetime.timedelta(days=n)返回值为 2 days, 0:00:00

'''

return datetime.date.today()-datetime.timedelta(days=n)

else:

return datetime.date.today()+datetime.timedelta(days=n)

def get_days_of_month(year,mon):

'''

get days of month

calender.monthrange()计算每个月的天数,返回一个元祖(0,31),此为2018年1月,第一个参数代表当月第一天是星期几,第二个参数代表是这个月的天数

'''

return calendar.monthrange(year,mon)[1]

def get_firstday_of_month(year,mon):

'''

get the first day of month

date format="YYYY-MM-DD"

'''

days="01"

if int(mon)<10:

#将一位数的月份左补0变为两位数

mon ="0"+str(int(mon))

arr = (year,mon,days)

return "_".join("%s"%i for i in arr)

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

def get_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 in arr)

def get_year_and_month(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:

#计算totalmon月的总天数

days = str(get_days_of_month(thisyear,totalmon))

#月份用0左补齐成两位数

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(get_days_of_month(thisyear,j))

j = addzero(j)

return str(thisyear),str(j),days

else:

if totalmon >0 and total <12:

days = str(get_days_of_month(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(get_days_of_month(thisyear,j))

j = addzero(j)

return str(thisyear),str(j),days

def get_today_month(n=0):

'''

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

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

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

date format = "YYYY-MM-DD"

'''

(y,m,d) = get_year_and_month(n)

arr = (y,m,d)

if int(day)

arr = (y,m,day)

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)=get_year_and_month(n)

d ="01"

arr =(y,m,d)

return "_".join("%s"%i for i in arr)

def main():

print('today is:',today())

print('today is:',todaystr())

print('the date time is:',datetime1())

print('data time is:',datetimestr())

print('2 days after today is:',get_day_of_day(2))

print('2 days before today is ',get_day_of_day(-2))

print('2 months after today is:',get_today_month(2))

print('2 months before today is:',get_today_month(-2))

print('2 months after this month is:',get_firstday_month(2))

print('2 months after this month is:',get_firstday_month(-2))

#__name__ 是当前模块名,当模块被直接运行时模块名为__main__.当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行

if __name__=="__main__":

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值