欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

最近因為工作原因,發(fā)現(xiàn)一個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中文亂碼的解決方法

    python中文亂碼的解決方法

    亂碼在哪種語言里都會出現(xiàn),今天給你二個解決python中文亂碼的方法。
    2013-11-11
  • python密碼學一次性密碼的實現(xiàn)

    python密碼學一次性密碼的實現(xiàn)

    這篇文章主要為大家介紹了python密碼學一次性密碼的實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • pycharm下載依賴一直失敗的問題踩坑指南

    pycharm下載依賴一直失敗的問題踩坑指南

    在使用pycharm學習python的時候,經常需要第三方庫,沒有第三方庫程序就會報錯,下面這篇文章主要給大家介紹了關于pycharm下載依賴一直失敗的問題踩坑指南,需要的朋友可以參考下
    2023-06-06
  • python 自動化辦公之批量修改文件名實操

    python 自動化辦公之批量修改文件名實操

    這篇文章主要介紹了python 自動化辦公之批量修改文件名實操,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Python結巴中文分詞工具使用過程中遇到的問題及解決方法

    Python結巴中文分詞工具使用過程中遇到的問題及解決方法

    這篇文章主要介紹了Python結巴中文分詞工具使用過程中遇到的問題及解決方法,較為詳細的講述了Python結巴中文分詞工具的下載、安裝、使用方法及容易出現(xiàn)的問題與相應解決方法,需要的朋友可以參考下
    2017-04-04
  • Python內建類型list源碼學習

    Python內建類型list源碼學習

    這篇文章主要為大家介紹了Python內建類型list源碼學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • matplotlib教程——強大的python作圖工具庫

    matplotlib教程——強大的python作圖工具庫

    這篇文章主要介紹了python matplotlib的相關資料,幫助大家更好的利用python matplotlib庫繪制圖表,感興趣的朋友可以了解下
    2020-10-10
  • 全CPU并行處理Pandas操作Pandarallel更快處理數據

    全CPU并行處理Pandas操作Pandarallel更快處理數據

    我們在處理數據時,通常小的數據對處理速度不敏感,但數據量一大,頓時會感覺數據處理效率不盡如人意,今天介紹的pandarallel就是一個簡單高效的Pandas并行工具,幾行代碼就可以提高數據處理效率,
    2024-01-01
  • python 爬取小說并下載的示例

    python 爬取小說并下載的示例

    這篇文章主要介紹了python 爬取小說并下載的示例,幫助大家更好的理解和學習python爬蟲,感興趣的朋友可以了解下
    2020-12-12
  • keras K.function獲取某層的輸出操作

    keras K.function獲取某層的輸出操作

    這篇文章主要介紹了keras K.function獲取某層的輸出操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評論