我正在使用datetime Python模块。我想计算从当前日期起6个月的日期。有人可以给我一些帮助吗?
我想要从当前日期起6个月生成日期的原因是生成审核日期。如果用户将数据输入系统,则其审核日期为自输入数据之日起6个月。
答案 0 :(得分:854)
我发现这个解决方案很好。 (这使用python-dateutil extension)
from datetime import date
from dateutil.relativedelta import relativedelta
six_months = date.today() + relativedelta(months=+6)
这种方法的优点在于它可以处理28,30,31天等问题。这在处理业务规则和方案(比如发票生成等)时非常有用。
$ date(2010,12,31)+relativedelta(months=+1)
datetime.date(2011, 1, 31)
$ date(2010,12,31)+relativedelta(months=+2)
datetime.date(2011, 2, 28)
答案 1 :(得分:46)
嗯,这取决于您当前日期后6个月的含义。
使用自然月份:
(day, month, year) = (day, (month+6)%12, year+(month+6)/12)
使用银行家的定义,6 * 30:
date += datetime.timedelta(6*30)
答案 2 :(得分:19)
import datetime
print (datetime.date.today() + datetime.timedelta(6*365/12)).isoformat()
答案 3 :(得分:17)
对于月初计算的开始:
from datetime import timedelta
from dateutil.relativedelta import relativedelta
end_date = start_date + relativedelta(months=delta_period) + timedelta(days=-delta_period)
答案 4 :(得分:14)
'6个月'是什么意思?是2009-02-13 + 6个月== 2009-08-13还是2009-02-13 + 6 * 30天?
import mx.DateTime as dt
#6 Months
dt.now()+dt.RelativeDateTime(months=6)
#result is '2009-08-13 16:28:00.84'
#6*30 days
dt.now()+dt.RelativeDateTime(days=30*6)
#result is '2009-08-12 16:30:03.35'
的更多信息
答案 5 :(得分:12)
使用Python 3.x,您可以这样做:
from datetime import datetime, timedelta
from dateutil.relativedelta import *
date = datetime.now()
print(date)
# 2018-09-24 13:24:04.007620
date = date + relativedelta(months=+6)
print(date)
# 2019-03-24 13:24:04.007620
,但您需要安装 python-dateutil 模块:
pip install python-dateutil
答案 6 :(得分:12)
所以,这里有一个dateutil.relativedelta
的例子,我发现它在过去的一年中有用,每次都跳过一个月到现在的日期:
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> today = datetime.datetime.today()
>>> month_count = 0
>>> while month_count < 12:
... day = today - relativedelta(months=month_count)
... print day
... month_count += 1
...
2010-07-07 10:51:45.187968
2010-06-07 10:51:45.187968
2010-05-07 10:51:45.187968
2010-04-07 10:51:45.187968
2010-03-07 10:51:45.187968
2010-02-07 10:51:45.187968
2010-01-07 10:51:45.187968
2009-12-07 10:51:45.187968
2009-11-07 10:51:45.187968
2009-10-07 10:51:45.187968
2009-09-07 10:51:45.187968
2009-08-07 10:51:45.187968
与其他答案一样,你必须在“6个月后”找出你的真正含义。如果你的意思是“未来六个月的今天这个月”,那么这就是:
datetime.datetime.now() + relativedelta(months=6)
答案 7 :(得分:12)
此解决方案适用于12月,此页面上的大部分答案都没有。 在使用模数(%)或整数除法(//)之前,您需要先将月份从基数1(即Jan = 1)转移到基数0(即Jan = 0),否则11月(11)加1个月会给出12 ,当找到余数(12%12)时给出0。
(并且不建议“(月%12)+ 1”或10月+ 1 = 12月!)
def AddMonths(d,x):
newmonth = ((( d.month - 1) + x ) % 12 ) + 1
newyear = d.year + ((( d.month - 1) + x ) / 12 )
return datetime.date( newyear, newmonth, d.day)
然而......这并没有像1月31日+一个月那样解决问题。所以我们回到OP - 添加一个月是什么意思?考虑到大多数人会认为jan的最后一天加上一个月等于2月的最后一天,一个soln将会回溯到有效的一天。 这也适用于负数月份。 证明:
>>> import datetime
>>> AddMonths(datetime.datetime(2010,8,25),1)
datetime.date(2010, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),4)
datetime.date(2010, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),5)
datetime.date(2011, 1, 25)
>>> AddMonths(datetime.datetime(2010,8,25),13)
datetime.date(2011, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),24)
datetime.date(2012, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-1)
datetime.date(2010, 7, 25)
>>> AddMonths(datetime.datetime(2010,8,25),0)
datetime.date(2010, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-12)
datetime.date(2009, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-8)
datetime.date(2009, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-7)
datetime.date(2010, 1, 25)>>>
答案 8 :(得分:11)
使用Python的日期时间没有直接的方法。
查看python-dateutil处的relativedelta类型。它允许您指定以月为单位的时间增量。
答案 9 :(得分:11)
我知道这已经持续了6个月,但是如果你要增加一个月,谷歌的答案显示“在python中增加几个月”:
import calendar
date = datetime.date.today() //Or your date
datetime.timedelta(days=calendar.monthrange(date.year,date.month)[1])
这将计算当月的天数,并将它们添加到当前日期,使用365/12,如果您的迭代超过该日期,则一年中的1/12会导致短/长月问题。
答案 10 :(得分:9)
只需使用 timetuple 方法提取月份,添加月份并构建新的日期对象。如果已有的方法,我不知道。
import datetime
def in_the_future(months=1):
year, month, day = datetime.date.today().timetuple()[:3]
new_month = month + months
return datetime.date(year + (new_month / 12), (new_month % 12) or 12, day)
API有点笨拙,但作为一个例子。显然也不会像2008-01-31 + 1个月那样在角落案件上工作。 :)
答案 11 :(得分:8)
Dateutil package已实现此类功能。但请注意,这将是天真,正如其他人已指出的那样。
答案 12 :(得分:7)
使用Python标准库,即没有import datetime
import calendar
def add_months(date, months):
months_count = date.month + months
# Calculate the year
year = date.year + int(months_count / 12)
# Calculate the month
month = (months_count % 12)
if month == 0:
month = 12
# Calculate the day
day = date.day
last_day_of_month = calendar.monthrange(year, month)[1]
if day > last_day_of_month:
day = last_day_of_month
new_date = datetime.date(year, month, day)
return new_date
或其他人,并解决“2月31日”问题。问题:
>>>date = datetime.date(2018, 11, 30)
>>>print(date, add_months(date, 3))
(datetime.date(2018, 11, 30), datetime.date(2019, 2, 28))
>>>print(date, add_months(date, 14))
(datetime.date(2018, 12, 31), datetime.date(2020, 2, 29))
测试:
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
IPAddress ip(192,168,1,200);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
答案 13 :(得分:4)
Python 有内置的库,请看下面的例子
import datetime
from dateutil.relativedelta import relativedelta
# subtract months
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_months = proc_dt + relativedelta(months=-3)
print(proc_dt_minus_3_months)
# add months
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_months = proc_dt + relativedelta(months=+3)
print(proc_dt_plus_3_months)
# subtract days:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_days = proc_dt + relativedelta(days=-3)
print(proc_dt_minus_3_days)
# add days days:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_days = proc_dt + relativedelta(days=+3)
print(proc_dt_plus_3_days)
# subtract years:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_years = proc_dt + relativedelta(years=-3)
print(proc_dt_minus_3_years)
# add years:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_years = proc_dt + relativedelta(years=+3)
print(proc_dt_plus_3_years)
结果:
2021-05-31
2021-11-30
2021-08-28
2021-09-03
2018-08-31
2024-08-31
答案 14 :(得分:4)
我有更好的方法解决'2月31日'的问题:
def add_months(start_date, months):
import calendar
year = start_date.year + (months / 12)
month = start_date.month + (months % 12)
day = start_date.day
if month > 12:
month = month % 12
year = year + 1
days_next = calendar.monthrange(year, month)[1]
if day > days_next:
day = days_next
return start_date.replace(year, month, day)
我认为它也适用于负数(减去月数),但我没有对此进行过多次测试。
答案 15 :(得分:2)
修改AddMonths()以在Zope中使用并处理无效的日期数字:
def AddMonths(d,x):
days_of_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
newmonth = ((( d.month() - 1) + x ) % 12 ) + 1
newyear = d.year() + ((( d.month() - 1) + x ) // 12 )
if d.day() > days_of_month[newmonth-1]:
newday = days_of_month[newmonth-1]
else:
newday = d.day()
return DateTime( newyear, newmonth, newday)
答案 16 :(得分:2)
import time
def add_month(start_time, months):
ret = time.strptime(start_time, '%Y-%m-%d')
t = list(ret)
t[1] += months
if t[1] > 12:
t[0] += 1 + int(months / 12)
t[1] %= 12
return int(time.mktime(tuple(t)))
答案 17 :(得分:2)
PyQt4的QDate类具有addmonths函数。
>>>from PyQt4.QtCore import QDate
>>>dt = QDate(2009,12,31)
>>>required = dt.addMonths(6)
>>>required
PyQt4.QtCore.QDate(2010, 6, 30)
>>>required.toPyDate()
datetime.date(2010, 6, 30)
答案 18 :(得分:1)
import datetime
'''
Created on 2011-03-09
@author: tonydiep
'''
def add_business_months(start_date, months_to_add):
"""
Add months in the way business people think of months.
Jan 31, 2011 + 1 month = Feb 28, 2011 to business people
Method: Add the number of months, roll back the date until it becomes a valid date
"""
# determine year
years_change = months_to_add / 12
# determine if there is carryover from adding months
if (start_date.month + (months_to_add % 12) > 12 ):
years_change = years_change + 1
new_year = start_date.year + years_change
# determine month
work = months_to_add % 12
if 0 == work:
new_month = start_date.month
else:
new_month = (start_date.month + (work % 12)) % 12
if 0 == new_month:
new_month = 12
# determine day of the month
new_day = start_date.day
if(new_day in [31, 30, 29, 28]):
#user means end of the month
new_day = 31
new_date = None
while (None == new_date and 27 < new_day):
try:
new_date = start_date.replace(year=new_year, month=new_month, day=new_day)
except:
new_day = new_day - 1 #wind down until we get to a valid date
return new_date
if __name__ == '__main__':
#tests
dates = [datetime.date(2011, 1, 31),
datetime.date(2011, 2, 28),
datetime.date(2011, 3, 28),
datetime.date(2011, 4, 28),
datetime.date(2011, 5, 28),
datetime.date(2011, 6, 28),
datetime.date(2011, 7, 28),
datetime.date(2011, 8, 28),
datetime.date(2011, 9, 28),
datetime.date(2011, 10, 28),
datetime.date(2011, 11, 28),
datetime.date(2011, 12, 28),
]
months = range(1, 24)
for start_date in dates:
for m in months:
end_date = add_business_months(start_date, m)
print("%s\t%s\t%s" %(start_date, end_date, m))
答案 19 :(得分:1)
获取x个月之后/之前的下一个日期的常规功能。
from datetime import date def after_month(given_date, month): yyyy = int(((given_date.year * 12 + given_date.month) + month)/12) mm = int(((given_date.year * 12 + given_date.month) + month)%12) if mm == 0: yyyy -= 1 mm = 12 return given_date.replace(year=yyyy, month=mm) if __name__ == "__main__": today = date.today() print(today) for mm in [-12, -1, 0, 1, 2, 12, 20 ]: next_date = after_month(today, mm) print(next_date)
答案 20 :(得分:1)
假设您的datetime变量名为date:
date=datetime.datetime(year=date.year+int((date.month+6)/12),
month=(date.month+6)%13 + (1 if (date.month +
months>12) else 0), day=date.day)
答案 21 :(得分:1)
在1new_month = 121的情况下修改了Johannes Wei的答案。这对我来说非常有用。这几个月可能是积极的,也可能是消极的。
def addMonth(d,months=1):
year, month, day = d.timetuple()[:3]
new_month = month + months
return datetime.date(year + ((new_month-1) / 12), (new_month-1) % 12 +1, day)
答案 22 :(得分:1)
以下是一个示例,它允许用户决定如何返回日期大于该月份天数的日期。
def add_months(date, months, endOfMonthBehaviour='RoundUp'):
assert endOfMonthBehaviour in ['RoundDown', 'RoundIn', 'RoundOut', 'RoundUp'], \
'Unknown end of month behaviour'
year = date.year + (date.month + months - 1) / 12
month = (date.month + months - 1) % 12 + 1
day = date.day
last = monthrange(year, month)[1]
if day > last:
if endOfMonthBehaviour == 'RoundDown' or \
endOfMonthBehaviour == 'RoundOut' and months < 0 or \
endOfMonthBehaviour == 'RoundIn' and months > 0:
day = last
elif endOfMonthBehaviour == 'RoundUp' or \
endOfMonthBehaviour == 'RoundOut' and months > 0 or \
endOfMonthBehaviour == 'RoundIn' and months < 0:
# we don't need to worry about incrementing the year
# because there will never be a day in December > 31
month += 1
day = 1
return datetime.date(year, month, day)
>>> from calendar import monthrange
>>> import datetime
>>> add_months(datetime.datetime(2016, 1, 31), 1)
datetime.date(2016, 3, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2)
datetime.date(2015, 12, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2, 'RoundDown')
datetime.date(2015, 11, 30)
答案 23 :(得分:1)
从this answer开始,请参阅parsedatetime。代码示例如下。更多详情:unit test with many natural-language -> YYYY-MM-DD conversion examples和明显的parsedatetime conversion challenges/bugs。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, calendar
from datetime import date
# from https://github.com/bear/parsedatetime
import parsedatetime as pdt
def print_todays_date():
todays_day_of_week = calendar.day_name[date.today().weekday()]
print "today's date = " + todays_day_of_week + ', ' + \
time.strftime('%Y-%m-%d')
def convert_date(natural_language_date):
cal = pdt.Calendar()
(struct_time_date, success) = cal.parse(natural_language_date)
if success:
formal_date = time.strftime('%Y-%m-%d', struct_time_date)
else:
formal_date = '(conversion failed)'
print '{0:12s} -> {1:10s}'.format(natural_language_date, formal_date)
print_todays_date()
convert_date('6 months')
以上代码从MacOSX计算机生成以下内容:
$ ./parsedatetime_simple.py
today's date = Wednesday, 2015-05-13
6 months -> 2015-11-13
$
答案 24 :(得分:1)
又一个解决方案 - 希望有人会喜欢它:
def add_months(d, months):
return d.replace(year=d.year+months//12).replace(month=(d.month+months)%12)
对于所有情况,此解决方案在第29,30,31天都不起作用,因此需要更强大的解决方案(不再那么好了:)):
def add_months(d, months):
for i in range(4):
day = d.day - i
try:
return d.replace(day=day).replace(year=d.year+int(months)//12).replace(month=(d.month+int(months))%12)
except:
pass
raise Exception("should not happen")
答案 25 :(得分:1)
这不能回答特定的问题(仅使用getchar
),但是,鉴于其他人建议使用不同的模块,这里有一个使用SELECT Master.Dbo.CAST(GETDATE() AS DATETIME2),UserDB.dbo.CAST(GETDATE() AS DATETIME2)
的解决方案。
Incorrect syntax near the keyword 'AS'.
在leap年中可以按预期工作
datetime
答案 26 :(得分:1)
“ python-dateutil”(外部扩展名)是一个很好的解决方案,但是您可以使用内置的Python模块(datetime和datetime)来实现
我编写了一个简短的代码来解决它(处理年,月,日)
(正在运行:Python 3.8.2)
from datetime import datetime
from calendar import monthrange
# Time to increase (in months)
inc = 12
# Returns mod of the division for 12 (months)
month = ((datetime.now().month + inc) % 12) or 1
# Increase the division by 12 (months), if necessary (+ 12 months increase)
year = datetime.now().year + int((month + inc) / 12)
# (IF YOU DON'T NEED DAYS,CAN REMOVE THE BELOW CODE)
# Returns the same day in new month, or the maximum day of new month
day = min(datetime.now().day,monthrange(year, month)[1])
print("Year: {}, Month: {}, Day: {}".format(year, month, day))
答案 27 :(得分:1)
我解决了这个问题:
import calendar
from datetime import datetime
moths2add = 6
now = datetime.now()
current_year = now.year
current_month = now.month
#count days in months you want to add using calendar module
days = sum(
[calendar.monthrange(current_year, elem)[1] for elem in range(current_month, current_month + moths)]
)
print now + days
答案 28 :(得分:0)
我迟到了,但是
查看Ken Reitz Maya模块,
https://github.com/kennethreitz/maya
像这样的事情可能对您有帮助,只需将小时= 1更改为天= 1或年份= 1
>>> from maya import MayaInterval
# Create an event that is one hour long, starting now.
>>> event_start = maya.now()
>>> event_end = event_start.add(hours=1)
>>> event = MayaInterval(start=event_start, end=event_end)
答案 29 :(得分:0)
一个快速的建议是Arrow
pipenv安装箭头
>>> import arrow
>>> arrow.now().date()
datetime.date(2019, 6, 28)
>>> arrow.now().shift(months=6).date()
datetime.date(2019, 12, 28)
答案 30 :(得分:0)
我经常需要一个月的最后一天才能保持一个月的最后一天。为了解决这个问题,我在计算前一天加了,然后在返回前又减去了。
from datetime import date, timedelta
# it's a lot faster with a constant day
DAY = timedelta(1)
def add_month(a_date, months):
"Add months to date and retain last day in month."
next_day = a_date + DAY
# calculate new year and month
m_sum = next_day.month + months - 1
y = next_day.year + m_sum // 12
m = m_sum % 12 + 1
try:
return date(y, m, next_day.day) - DAY
except ValueError:
# on fail return last day in month
# can't fail on december so I don't bother changing the year
return date(y, m + 1, 1) - DAY
答案 31 :(得分:0)
我使用了replace()
方法并编写了此递归函数。 dt
是datetime.datetime
对象:
def month_timedelta(dt, m):
y = m // 12
dm = m % 12
if y == 0:
if dt.month + m <= 12:
return dt.replace(month = dt.month + m)
else:
dy = (dt.month + m) // 12
ndt = dt.replace(year=dt.year + dy)
return ndt.replace(month=(ndt.month + m) % 12)
else:
return month_timedelta(dt.replace(year=dt.year + y),dm)
答案 32 :(得分:0)
我的实现基于 taleinat 的回答:
import datetime
import calendar
def add_months(orig_date, month_count = 1):
while month_count > 12:
month_count -= 12
orig_date = add_months(orig_date, 12)
new_year = orig_date.year
new_month = orig_date.month + month_count
# note: in datetime.date, months go from 1 to 12
if new_month > 12:
new_year += 1
new_month -= 12
last_day_of_month = calendar.monthrange(new_year, new_month)[1]
new_day = min(orig_date.day, last_day_of_month)
return orig_date.replace(year=new_year, month=new_month, day=new_day)
使用此功能,您可以添加任意数量的月份。
from datetime import date
dt = date(2021, 1, 31)
print(add_months(dt, 49))
返回
2025-02-28
答案 33 :(得分:0)
使用python datetime模块向datetime.today()添加六个月的timedelta。
http://docs.python.org/library/datetime.html
你当然要解决JohannesWeiß提出的问题 - 你的意思是6个月做什么?
答案 34 :(得分:0)
这就是我想出的。它移动了正确的月数和年数,但忽略了几天(这是我在我的情况下所需要的)。
import datetime
month_dt = 4
today = datetime.date.today()
y,m = today.year, today.month
m += month_dt-1
year_dt = m//12
new_month = m%12
new_date = datetime.date(y+year_dt, new_month+1, 1)
答案 35 :(得分:0)
另一种解决方案:计算下个月n个月的天数总和,并将结果添加到当前日期。
import calendar
import datetime
def date_from_now(months):
today = datetime.datetime.today()
month = today.month
year = today.year
sum_days = 0
for i in range(int(months)):
month += 1
if month == 13:
month = 1
year += 1
sum_days += calendar.monthrange(year, month)[1]
return datetime.date.today() + datetime.timedelta(sum_days)
print(date_from_now(12)) # if to day is 2017-01-01, output: 2019-01-01
答案 36 :(得分:0)
user417751对早期答案的返工。也许不是那么pythonic方式,但它照顾不同的月份长度和闰年。在这种情况下,2012年1月31日+ 1个月= 2012年2月29日。
import datetime
import calendar
def add_mths(d, x):
newday = d.day
newmonth = (((d.month - 1) + x) % 12) + 1
newyear = d.year + (((d.month - 1) + x) // 12)
if newday > calendar.mdays[newmonth]:
newday = calendar.mdays[newmonth]
if newyear % 4 == 0 and newmonth == 2:
newday += 1
return datetime.date(newyear, newmonth, newday)
答案 37 :(得分:0)
我使用此功能更改年份和月份但保留日期:
def replace_month_year(date1, year2, month2):
try:
date2 = date1.replace(month = month2, year = year2)
except:
date2 = datetime.date(year2, month2 + 1, 1) - datetime.timedelta(days=1)
return date2
你应该写:
new_year = my_date.year + (my_date.month + 6) / 12
new_month = (my_date.month + 6) % 12
new_date = replace_month_year(my_date, new_year, new_month)
答案 38 :(得分:0)
我认为做这样的事情会更安全,而不是手动添加天数:
import datetime
today = datetime.date.today()
def addMonths(dt, months = 0):
new_month = months + dt.month
year_inc = 0
if new_month>12:
year_inc +=1
new_month -=12
return dt.replace(month = new_month, year = dt.year+year_inc)
newdate = addMonths(today, 6)
答案 39 :(得分:-1)
使用以下给定的功能,您可以在x个月之后/之前获得日期。
from datetime import date def next_month(given_date, month): yyyy = int(((given_date.year * 12 + given_date.month) + month)/12) mm = int(((given_date.year * 12 + given_date.month) + month)%12) if mm == 0: yyyy -= 1 mm = 12 return given_date.replace(year=yyyy, month=mm) if __name__ == "__main__": today = date.today() print(today) for mm in [-12, -1, 0, 1, 2, 12, 20 ]: next_date = next_month(today, mm) print(next_date)
答案 40 :(得分:-1)
我无法找到这个问题的确切解决方案,因此我会发布我的解决方案,以防使用标准日历和日期时间库可能有任何帮助。这适用于添加和减去月份,以及月末滚动的帐户以及最后一个月比初始月份少的天数。如果您正在寻找更复杂的操作,我还有一个更通用的解决方案,它会定期添加(天,月,年,季,章等),如:&#39; 1m&#39;,&#39; -9m& #39;,&#39; -1.5y&#39;,&#39; -3q&#39;,&#39; 1s&#39;等。
sizeof(char*)
答案 41 :(得分:-1)
当我需要添加数月或数年并且不想导入更多库时,我就会这样做。
import datetime
__author__ = 'Daniel Margarido'
# Check if the int given year is a leap year
# return true if leap year or false otherwise
def is_leap_year(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return True
else:
return False
else:
return True
else:
return False
THIRTY_DAYS_MONTHS = [4, 6, 9, 11]
THIRTYONE_DAYS_MONTHS = [1, 3, 5, 7, 8, 10, 12]
# Inputs -> month, year Booth integers
# Return the number of days of the given month
def get_month_days(month, year):
if month in THIRTY_DAYS_MONTHS: # April, June, September, November
return 30
elif month in THIRTYONE_DAYS_MONTHS: # January, March, May, July, August, October, December
return 31
else: # February
if is_leap_year(year):
return 29
else:
return 28
# Checks the month of the given date
# Selects the number of days it needs to add one month
# return the date with one month added
def add_month(date):
current_month_days = get_month_days(date.month, date.year)
next_month_days = get_month_days(date.month + 1, date.year)
delta = datetime.timedelta(days=current_month_days)
if date.day > next_month_days:
delta = delta - datetime.timedelta(days=(date.day - next_month_days) - 1)
return date + delta
def add_year(date):
if is_leap_year(date.year):
delta = datetime.timedelta(days=366)
else:
delta = datetime.timedelta(days=365)
return date + delta
# Validates if the expected_value is equal to the given value
def test_equal(expected_value, value):
if expected_value == value:
print "Test Passed"
return True
print "Test Failed : " + str(expected_value) + " is not equal to " str(value)
return False
# Test leap year
print "---------- Test leap year ----------"
test_equal(True, is_leap_year(2012))
test_equal(True, is_leap_year(2000))
test_equal(False, is_leap_year(1900))
test_equal(False, is_leap_year(2002))
test_equal(False, is_leap_year(2100))
test_equal(True, is_leap_year(2400))
test_equal(True, is_leap_year(2016))
# Test add month
print "---------- Test add month ----------"
test_equal(datetime.date(2016, 2, 1), add_month(datetime.date(2016, 1, 1)))
test_equal(datetime.date(2016, 6, 16), add_month(datetime.date(2016, 5, 16)))
test_equal(datetime.date(2016, 3, 15), add_month(datetime.date(2016, 2, 15)))
test_equal(datetime.date(2017, 1, 12), add_month(datetime.date(2016, 12, 12)))
test_equal(datetime.date(2016, 3, 1), add_month(datetime.date(2016, 1, 31)))
test_equal(datetime.date(2015, 3, 1), add_month(datetime.date(2015, 1, 31)))
test_equal(datetime.date(2016, 3, 1), add_month(datetime.date(2016, 1, 30)))
test_equal(datetime.date(2016, 4, 30), add_month(datetime.date(2016, 3, 30)))
test_equal(datetime.date(2016, 5, 1), add_month(datetime.date(2016, 3, 31)))
# Test add year
print "---------- Test add year ----------"
test_equal(datetime.date(2016, 2, 2), add_year(datetime.date(2015, 2, 2)))
test_equal(datetime.date(2001, 2, 2), add_year(datetime.date(2000, 2, 2)))
test_equal(datetime.date(2100, 2, 2), add_year(datetime.date(2099, 2, 2)))
test_equal(datetime.date(2101, 2, 2), add_year(datetime.date(2100, 2, 2)))
test_equal(datetime.date(2401, 2, 2), add_year(datetime.date(2400, 2, 2)))
只需创建一个datetime.date()对象,调用add_month(date)添加一个月,使用add_year(date)添加一年。
答案 42 :(得分:-1)
def addDay(date, number):
for i in range(number)
#try to add a day
try:
date = date.replace(day = date.day + 1)
#in case it's impossible ex:january 32nd add a month and restart at day 1
except:
#add month part
try:
date = date.replace(month = date.month +1, day = 1)
except:
date = date.replace(year = date.year +1, month = 1, day = 1)
对于仍在阅读这篇文章的所有人。我认为这段代码更清晰,特别是与使用modulo(%)的代码相比。
对不起任何语法错误,英语不是我的主要语言
答案 43 :(得分:-1)
我们可能应该使用dateutil.relativedelta
然而,为了学术兴趣,我会在发现它之前添加它,我是goint使用它:
尝试:
vexpDt = K.today.replace(K.today.year +(K.today.month + 6)// 12,(K.today.month + 5)%12 + 1,K.today.day)
除了:
vexpDt = K.today.replace(K.today.year +(K.today.month + 6)// 12,(K.today.month + 6)%12 + 1,1) - timedelta(days = 1)
看起来很简单,但仍然可以解决所有问题,如29,30,31
它通过-timedelta
工作 - 6个月nb - 不要被K.today混淆它只是我程序中的一个变量
答案 44 :(得分:-1)
在此功能中,n可以是正数或负数。
def addmonth(d, n):
n += 1
dd = datetime.date(d.year + n/12, d.month + n%12, 1)-datetime.timedelta(1)
return datetime.date(dd.year, dd.month, min(d.day, dd.day))
答案 45 :(得分:-1)
我对Tony Diep的回答进行了修改,可能稍微优雅一点(Python 2当然,匹配问题的日期和原始答案,Python 3必要时修改,包括/
到//
至少):
def add_months(date, months):
month = date.month + months - 1
year = date.year + (month / 12)
month = (month % 12) + 1
day = date.day
while (day > 0):
try:
new_date = date.replace(year=year, month=month, day=day)
break
except:
day = day - 1
return new_date
根据“业务需求”解释添加月份,该解释将日期映射到月末,应映射到月末而不是下个月。