如何在不使用库的情况下在python中按自定义月份增加日期时间 [英] How to increment datetime by custom months in python without using library

查看:20
本文介绍了如何在不使用库的情况下在python中按自定义月份增加日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加日期时间值的月份

next_month = datetime.datetime(mydate.year, mydate.month+1, 1)

当月份为 12 时,它变为 13 并引发错误月份必须在 1..12".(我预计年份会增加)

我想使用 timedelta,但它不需要月份参数.有 relativedelta python 包,但我不想仅为此安装它.还有一个使用 strtotime 的解决方案.

time = strtotime(str(mydate));next_month = date("Y-m-d", strtotime("+1 个月", time));

我不想从日期时间转换为 str 然后转换为时间,然后转换为日期时间;因此,它仍然是一个图书馆

有没有人有任何好的和简单的解决方案,就像使用 timedelta 一样?

解决方案

Edit - 根据您的评论,如果下个月的天数较少,则需要四舍五入的日期,这里是一个解决方案:

导入日期时间导入日历def add_months(sourcedate,months):月 = sourcedate.month - 1 + 月年 = 源日期.年 + 月//12月 = 月 % 12 + 1day = min(sourcedate.day, calendar.monthrange(year,month)[1])返回日期时间.日期(年、月、日)

正在使用中:

<预><代码>>>>somedate = datetime.date.today()>>>某天datetime.date(2010, 11, 9)>>>add_months(somedate,1)datetime.date(2010, 12, 9)>>>add_months(somedate,23)datetime.date(2012, 10, 9)>>>otherdate = datetime.date(2010,10,31)>>>add_months(otherdate,1)datetime.date(2010, 11, 30)

此外,如果您不担心小时、分钟和秒,您可以使用 date 而不是 datetime.如果您担心小时、分钟和秒,您需要修改我的代码以使用 datetime 并将小时、分钟和秒从源复制到结果.

I need to increment the month of a datetime value

next_month = datetime.datetime(mydate.year, mydate.month+1, 1)

when the month is 12, it becomes 13 and raises error "month must be in 1..12". (I expected the year would increment)

I wanted to use timedelta, but it doesn't take month argument. There is relativedelta python package, but i don't want to install it just only for this. Also there is a solution using strtotime.

time = strtotime(str(mydate));
next_month = date("Y-m-d", strtotime("+1 month", time));

I don't want to convert from datetime to str then to time, and then to datetime; therefore, it's still a library too

Does anyone have any good and simple solution just like using timedelta?

解决方案

Edit - based on your comment of dates being needed to be rounded down if there are fewer days in the next month, here is a solution:

import datetime
import calendar

def add_months(sourcedate, months):
    month = sourcedate.month - 1 + months
    year = sourcedate.year + month // 12
    month = month % 12 + 1
    day = min(sourcedate.day, calendar.monthrange(year,month)[1])
    return datetime.date(year, month, day)

In use:

>>> somedate = datetime.date.today()
>>> somedate
datetime.date(2010, 11, 9)
>>> add_months(somedate,1)
datetime.date(2010, 12, 9)
>>> add_months(somedate,23)
datetime.date(2012, 10, 9)
>>> otherdate = datetime.date(2010,10,31)
>>> add_months(otherdate,1)
datetime.date(2010, 11, 30)

Also, if you're not worried about hours, minutes and seconds you could use date rather than datetime. If you are worried about hours, minutes and seconds you need to modify my code to use datetime and copy hours, minutes and seconds from the source to the result.

这篇关于如何在不使用库的情况下在python中按自定义月份增加日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆