python日期操作类代码-创新互联
完整代码
创新互联公司-专业网站定制、快速模板网站建设、高性价比嵊州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式嵊州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖嵊州地区。费用合理售后完善,十多年实体公司更值得信赖。# -*- coding: utf-8 -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime import timedelta, date import calendar year = strftime("%Y",localtime()) mon = strftime("%m",localtime()) day = strftime("%d",localtime()) hour = strftime("%H",localtime()) min = strftime("%M",localtime()) sec = strftime("%S",localtime()) def today(): ''''' get today,date format="YYYY-MM-DD" ''''' return date.today() def todaystr(): ''' get date string, date format="YYYYMMDD" ''' return year+mon+day def datetime(): ''''' get datetime,format="YYYY-MM-DD HH:MM:SS" ''' return strftime("%Y-%m-%d %H:%M:%S",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) return date.today()-timedelta(days=n) else: return date.today()+timedelta(days=n) def get_days_of_month(year,mon): ''''' get days of month ''' 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): mon = "0"+str(int(mon)) arr = (year,mon,days) return "-".join("%s" %i for i in arr) 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_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 in arr) 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 in getyearandmonth(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+n if(n>=0): if(totalmon<=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) 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/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 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_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)另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前文章:python日期操作类代码-创新互联
网站路径:http://hbruida.cn/article/cosphi.html