Python Gets the date n days or n months before and after the current date

Source: Internet
Author: User
Tags timedelta

#-*-coding:utf-8-*-" "Gets the date of n days or n months before and after the current date" " fromTimeImportstrftime, LocalTime fromDatetimeImportTimedelta, DateImportCalendarYear= 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"" "    returnyear+mon+ Daydefdatetime ():" "" get datetime,format=" Yyyy-mm-dd HH:MM:SS "" "    returnStrftime ("%y-%m-%d%h:%m:%s", LocalTime ())defdatetimestr ():" "' Get datetime string date format= ' Yyyymmddhhmmss '" "    returnyear+mon+day+hour+min+secdefGet_day_of_day (n=0):" "" if N>=0,date is larger than today if n<0,date are less than today date format =" Yyyy-mm-dd "" "    if(n<0): N=ABS (N)returnDate.today ()-timedelta (days=N)Else:        returnDate.today () +timedelta (days=N)defGet_days_of_month (Year,mon):" "' Get days of month '" "     returnCalendar.monthrange (year, Mon) [1]   defGet_firstday_of_month (Year,mon):" "' Get the first day of month date format = ' Yyyy-mm-dd '" " Days=" on"     if(int (Mon) <10): Mon="0"+str (int (Mon)) Arr=(year,mon,days)return "-". Join ("%s"%i forIincharr)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 forIincharr)defGet_firstday_month (n=0):" "' Get the first day of month from today N was how many months" "(y,m,d)=getyearandmonth (n) d=" on"arr=(y,m,d)return "-". Join ("%s"%i forIincharr)defGet_lastday_month (n=0):" ""Get the last day of this month from today N was how many months" "     return "-". Join ("%s"%i forIinchgetyearandmonth (n))defGetyearandmonth (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%12if(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%12if(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:         returnnabsdefGet_today_month (n=0):" ""Gets the date of N-month before and after the current date if n>0, gets the date of the N-month before the current date if n<0, gets the date of the N-month after the current date format =" Yyyy-mm-dd "" "(y,m,d)=getyearandmonth (n) Arr=(y,m,d)if(Int (day) <Int (d)): Arr=(Y,m,day)return "-". Join ("%s"%i forIincharr)if __name__=="__main__":    Printtoday ()Printtodaystr ()Printdatetime ()Printdatetimestr ()PrintGet_day_of_day (20)    PrintGet_day_of_day (-3)    PrintGet_today_month (-3)    PrintGet_today_month (3)

Python Gets the date n days or n months before and after the current date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.