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)<int(d):
  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()
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM