时间:2021-05-22
完整代码
# -*- coding: utf-8 -*-'''获取当前日期前后N天或N月的日期'''from time import strftime, localtimefrom datetime import timedelta, dateimport calendaryear = 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+daydef 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+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)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)<int(d)): arr = (y,m,day) return "-".join("%s" %i for i in arr) if __name__=="__main__": print today() print todaystr() print datetime() print datetimestr() print get_day_of_day(20) print get_day_of_day(-3) print get_today_month(-3) print get_today_month(3)这篇关于python 日期操作类的文章就介绍到这,里面涉及了python日期操作的一些基础知识。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了python实现简单日期工具类的具体代码,供大家参考,具体内容如下importdatetimeimporttimeDATETIME_FORM
本文实例讲述了Java日期操作类常见用法。分享给大家供大家参考,具体如下:一取出当前日期时间1代码importjava.time.*;publicclassGe
复制代码代码如下://////计算日期的间隔(静态类)///publicstaticclassdateTimeDiff{#region计算日期间隔//////计
js代码有一百多行。先上效果图html代码日期:设置input元素类名为hhm-dateInputer,通过这个类来绑定这个日期输入控件。js代码这里应用了jQ
本文实例讲述了python日期相关操作。分享给大家供大家参考,具体如下:用Python做项目时,经常会遇到与日期转换相关,日期计算相关的功能,动不动就要去查py