最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年07月30日 08:24:41   投稿:daisy  
最近在工作中遇到一個需求,查找資料發(fā)現了一個很好的時間組件,所以下面這篇文章主要給大家介紹了關于利用python獲取當前日期前后N天或N月日期的方法示例,需要的朋友們可以參考借鑒,下面來一起看看吧。

前言

最近因為工作原因,發(fā)現一個Python的時間組件,很好用分享出來?。ㄍ涀髡呙至?,在這里先感謝了),下面話不多說,來一起看看詳細的介紹吧。

示例代碼:

# -*- 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) < 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)

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持

相關文章

  • Python實現蟻群優(yōu)化算法的示例代碼

    Python實現蟻群優(yōu)化算法的示例代碼

    蟻群算法是一種源于大自然生物世界的新的仿生進化算法,本文主要介紹了Python如何實現蟻群算法,文中通過示例代碼具有一定的參考價值,感興趣的小伙伴們可以了解一下
    2023-08-08
  • OpenCV?讀取圖像imread的使用詳解

    OpenCV?讀取圖像imread的使用詳解

    這篇文章主要介紹了OpenCV?讀取圖像imread的使用詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • python實現合并兩個數組的方法

    python實現合并兩個數組的方法

    這篇文章主要介紹了python實現合并兩個數組的方法,實例分析了兩種常用的合并數組的技巧,非常簡單實用,需要的朋友可以參考下
    2015-05-05
  • Python發(fā)送email的3種方法

    Python發(fā)送email的3種方法

    這篇文章主要介紹了Python發(fā)送email的3種方法,本文講解了使用登錄郵件服務器方法、調用sendmail命令、使用smtp服務來發(fā)送三種方法,需要的朋友可以參考下
    2015-04-04
  • Python使用defaultdict解決字典默認值

    Python使用defaultdict解決字典默認值

    本文主要介紹了Python使用defaultdict解決字典默認值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • matplotlib quiver箭圖繪制案例

    matplotlib quiver箭圖繪制案例

    這篇文章主要介紹了matplotlib quiver箭圖繪制案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python 實現文件讀寫、坐標尋址、查找替換功能

    Python 實現文件讀寫、坐標尋址、查找替換功能

    這篇文章主要介紹了Python 實現文件讀寫、坐標尋址、查找替換功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2019-09-09
  • 利用python對月餅數據進行可視化(看看哪家最劃算)

    利用python對月餅數據進行可視化(看看哪家最劃算)

    通過python對數據進行可視化展示,可直觀地展示數據之間的關系,為用戶提供更多的信息,這篇文章主要給大家介紹了關于利用python對月餅數據進行可視化的相關資料,看看哪家最劃算,需要的朋友可以參考下
    2022-09-09
  • 淺析Python中常見數據脫敏技術應用與對比

    淺析Python中常見數據脫敏技術應用與對比

    數據脫敏通過對敏感數據進行轉換,確保其在保護隱私的同時仍能用于開發(fā),本文為大家整理了一些常見的數據脫敏技術,感興趣的小伙伴可以了解下
    2025-02-02
  • Python格式化輸出之format用法詳解

    Python格式化輸出之format用法詳解

    Python中格式化字符串目前有兩種陣營:%和format,這篇文章主要給大家介紹了關于Python格式化輸出之format用法的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01

最新評論

阜城县| 湖州市| 江永县| 德钦县| 乌拉特前旗| 屏东县| 龙山县| 始兴县| 辛集市| 都昌县| 阿坝| 北碚区| 翁源县| 谷城县| 天柱县| 苍山县| 司法| 祁连县| 双柏县| 杭州市| 子长县| 丹凤县| 中超| 高平市| 高青县| 威远县| 开远市| 黄大仙区| 永城市| 临沂市| 绥芬河市| 舟曲县| 肇东市| 丰镇市| 微博| 桂东县| 西和县| 平和县| 诸暨市| 博罗县| 娱乐|