日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python获取一个月之前日期_利用python获取当前日期前后N天或N月日期的方法示例...

發布時間:2023/12/3 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获取一个月之前日期_利用python获取当前日期前后N天或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获取当前日期前后N天或N月日期的方法示例...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。