国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用python獲取當前日期前后N天或N月日期的方法示例

2020-02-16 01:59:29
字體:
來源:轉載
供稿:網友

前言

最近因為工作原因,發現一個Python的時間組件,很好用分享出來!(忘記作者名字了,在這里先感謝了),下面話不多說,來一起看看詳細的介紹吧。

示例代碼:

# -*- 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 nabsdef 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) print get_today_month(19)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌邑市| 扶风县| 伊金霍洛旗| 札达县| 砀山县| 丁青县| 黑河市| 长顺县| 白银市| 三门峡市| 蒙自县| 广安市| 泾川县| 红原县| 新巴尔虎右旗| 安庆市| 始兴县| 社旗县| 乌拉特后旗| 凤城市| 龙州县| 饶平县| 温州市| 海原县| 汤原县| 郑州市| 枞阳县| 阳谷县| 新龙县| 资中县| 新干县| 开封县| 东乌珠穆沁旗| 云梦县| 丽水市| 深泽县| 察隅县| 疏勒县| 长顺县| 天祝| 荥阳市|