python中計算上個月和下個月的第一天的方法

2021-09-08 11:54:44 字數 1094 閱讀 2833

鬧騰,乙個簡單的東西複雜化了,記錄下吧:

import

datetime,time

defget_1st_of_last_month():

"""獲取上個月第一天的日期,然後加21天就是22號的日期

:return: 返回日期

"""today=datetime.datetime.today()

year=today.year

month=today.month

if month==1:

month=12year-=1

else

: month-=1res=datetime.datetime(year,month,1)+datetime.timedelta(days=21)

return

res#

############################

defget_1st_of_next_month():

"""獲取下個月的22號的日期

:return: 返回日期

"""today=datetime.datetime.today()

year=today.year

month=today.month

if month==12:

month=1year+=1

else

: month+=1res=datetime.datetime(year,month,1)+datetime.timedelta(days=21)

return

res#

############################

deftime_s_to_stamp(args):

"""將datetime日期格式,先timetuple()轉化為struct_time格式

然後time.mktime轉化為時間戳

:param args: datetime時間格式資料

:return: 時間戳格式資料

"""res=time.mktime(args.timetuple())

return res

view code

Mysql 計算上個月最後一天 上上個月最後一天

select date format now y m d interval day date format now y m d dayas 上個月最後一天 date format date add now interval 1 month y m d interval day date format...

JS獲取下個月或上個月的今天

date.prototype.format function fmt if y test fmt for var k in o return fmt 呼叫方式 var time1 new date format yyyy mm dd hh mm ss 為了方便就封裝成傳遞乙個天數,即可獲取多少天後的...

js獲取上個月的今天或者下個月的今天

如果上個月或者下個月沒有今天,就取上個月或者下個月的最後一天。傳引數 1是下個月的今天,1是取上個月的今天。function getxmonthtoday type if parseint day 10 now year month day 如果取當月日期可直接 return 返回 var prem...