2010-02-12 37 views
29

獲得Python中的某一天的下月的同一天,我知道使用datetime.timedelta我可以得到一些天遠形式給出日期如何使用日期時間

daysafter = datetime.date.today() + datetime.timedelta(days=5) 

日期,但似乎沒有日期時間。 timedelta(月= 1)

!感謝您的幫助!

回答

25

當然沒有 - 如果今日1月31日,這將是「下個月的同一天」?顯然沒有的解決方案,因爲2月31日不存在,而datetime模塊確實不是發揮在「猜測什麼用戶提出這個不可能的問題沒有一個正確的解決方案認爲(錯誤)是明顯的解決方案」; - )。

我建議:

try: 
    nextmonthdate = x.replace(month=x.month+1) 
except ValueError: 
    if x.month == 12: 
    nextmonthdate = x.replace(year=x.year+1, month=1) 
    else: 
    # next month is too short to have "same date" 
    # pick your own heuristic, or re-raise the exception: 
    raise 
+1

感謝亞歷克斯!我曾經在c#中使用Datetime.AddMonths()來解決這個問題。所以在問轉儲問題之前,我甚至都沒有多想:)。 我與C#Datetime.AddMonths(1)月(28-31),有趣的結果的測試,我得到了2月5日28 再次感謝亞歷克斯! – icn 2010-02-12 06:38:12

82

使用dateutil模塊。它有relative time deltas

import datetime 
from dateutil import relativedelta 
nextmonth = datetime.date.today() + relativedelta.relativedelta(months=1) 

美麗。

+1

dateutil真棒。應該是標準的。 – Max 2012-02-14 15:51:34

+4

哇它處理1月31日+ 1個月。它返回了2月的最後一天 – 2015-07-10 06:53:44

+0

這應該是被接受的答案。所以要簡單得多...... – Someguy123 2017-09-02 17:32:29

2
from calendar import mdays 
from datetime import datetime, timedelta 

today = datetime.now() 
next_month_of_today = today + timedelta(mdays[today.month]) 

我不想導入dateutil。試試這個。祝你好運。

+1

我真的想這個解決方案的工作,但2月4日的一年將被打破使用mdays ... – Zemogle 2012-04-12 02:18:57

+0

mdays甚至沒有暴露在文檔中,可能是因爲mdays是不準確的閏年。 '月度範圍'功能更準確,因爲它考慮到了這一點。 – 2012-05-16 21:02:32

5
import calendar, datetime 

def next_month (date): 
    """return a date one month in advance of 'date'. 
    If the next month has fewer days then the current date's month, this will return an 
    early date in the following month.""" 
    return date + datetime.timedelta(days=calendar.monthrange(date.year,date.month)[1]) 
+1

這是錯誤的。 'next_month(datetime.date(2014,01,30))== datetime.date(2014,3,2)' – fjsj 2014-09-01 22:11:02

0
from datetime import timedelta 
try: 
    next_month = (x.replace(day=28) + timedelta(days=7)).replace(day=x.day) 
except ValueError: # assuming January 31 should return last day of February. 
    next_month = (x + timedelta(days=31)).replace(day=1) - timedelta(days=1) 
0

這是我如何解決它。

from datetime import date 
try: 
    (year, month) = divmod(date.today().month, 12) 
    next_month = date.today().replace(year=date.today().year+year, month=month+1) 
except ValueError: 
    # This day does not exist in next month 

可以跳過try/catch語句,如果你只通過設置replace(year=date.today().year+year, month=month, day=1)想的第一天下個月。這將永遠是因爲我們已經趕上使用divmod月份溢出有效日期。

1

這項工作對我來說

import datetime 
import calendar 


def next_month_date(d): 
    _year = d.year+(d.month//12) 
    _month = 1 if (d.month//12) else d.month + 1 
    next_month_len = calendar.monthrange(_year,_month)[1] 
    next_month = d 
    if d.day > next_month_len: 
     next_month = next_month.replace(day=next_month_len) 
    next_month = next_month.replace(year=_year, month=_month) 
    return next_month 

用法:

d = datetime.datetime.today() 
print next_month_date(d) 
相關問題