查找两个日期之间月份的最​​佳方法 [英] Best way to find the months between two dates

查看:43
本文介绍了查找两个日期之间月份的最​​佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在 python 中准确地找到两个日期之间的月份.我有一个有效的解决方案,但它不是很好(如优雅)或快速.

I have the need to be able to accurately find the months between two dates in python. I have a solution that works but its not very good (as in elegant) or fast.

dateRange = [datetime.strptime(dateRanges[0], "%Y-%m-%d"), datetime.strptime(dateRanges[1], "%Y-%m-%d")]
months = [] 

tmpTime = dateRange[0]
oneWeek = timedelta(weeks=1)
tmpTime = tmpTime.replace(day=1)
dateRange[0] = tmpTime
dateRange[1] = dateRange[1].replace(day=1)
lastMonth = tmpTime.month
months.append(tmpTime)
while tmpTime < dateRange[1]:
    if lastMonth != 12:
        while tmpTime.month <= lastMonth:
            tmpTime += oneWeek
        tmpTime = tmpTime.replace(day=1)
        months.append(tmpTime)
        lastMonth = tmpTime.month

    else:
        while tmpTime.month >= lastMonth:
            tmpTime += oneWeek
        tmpTime = tmpTime.replace(day=1)
        months.append(tmpTime)
        lastMonth = tmpTime.month

所以只是为了解释一下,我在这里做的是获取两个日期并将它们从 iso 格式转换为 python datetime 对象.然后我循环添加一个星期到开始日期时间对象并检查月份的数值是否更大(除非月份是十二月然后它检查日期是否更小),如果值更大我将它附加到列表中几个月,并不断循环,直到我到达结束日期.

So just to explain, what I'm doing here is taking the two dates and converting them from iso format into python datetime objects. Then I loop through adding a week to the start datetime object and check if the numerical value of the month is greater (unless the month is December then it checks if the date is less), If the value is greater I append it to the list of months and keep looping through until I get to my end date.

它工作得很好,只是看起来不是一个好方法......

It works perfectly it just doesn't seem like a good way of doing it...

推荐答案

更新 2018-04-20: 似乎 OP @Joshkunz 要求查找哪几个月在两个日期之间,而不是多少个月"在两个日期之间.所以我不确定为什么@JohnLaRooy 被投票超过 100 次.@Joshkunz 在原始问题下的评论中指出,他想要实际日期 [或月份],而不是查找月份总数.

Update 2018-04-20: it seems that OP @Joshkunz was asking for finding which months are between two dates, instead of "how many months" are between two dates. So I am not sure why @JohnLaRooy is upvoted for more than 100 times. @Joshkunz indicated in the comment under the original question he wanted the actual dates [or the months], instead of finding the total number of months.

所以看起来需要的问题是在两个日期之间 2018-04-112018-06-01

So it appeared the question wanted, for between two dates 2018-04-11 to 2018-06-01

Apr 2018, May 2018, June 2018 

如果在 2014-04-112018-06-01 之间呢?那么答案是

And what if it is between 2014-04-11 to 2018-06-01? Then the answer would be

Apr 2014, May 2014, ..., Dec 2014, Jan 2015, ..., Jan 2018, ..., June 2018

这就是我多年前拥有以下伪代码的原因.它只是建议使用两个月作为终点并循环遍历它们,一次增加一个月.@Joshkunz 提到他想要月份",他还提到他想要日期",但在不确切知道的情况下,很难编写确切的代码,但其想法是使用一个简单的循环来遍历端点,并且一次递增一个月.

So that's why I had the following pseudo code many years ago. It merely suggested using the two months as end points and loop through them, incrementing by one month at a time. @Joshkunz mentioned he wanted the "months" and he also mentioned he wanted the "dates", without knowing exactly, it was difficult to write the exact code, but the idea is to use one simple loop to loop through the end points, and incrementing one month at a time.

8 年前的 2010 年的答案:

The answer 8 years ago in 2010:

如果增加一周,那么它将根据需要大约执行 4.35 倍的工作.为什么不只是:

If adding by a week, then it will approximately do work 4.35 times the work as needed. Why not just:

1. get start date in array of integer, set it to i: [2008, 3, 12], 
       and change it to [2008, 3, 1]
2. get end date in array: [2010, 10, 26]
3. add the date to your result by parsing i
       increment the month in i
       if month is >= 13, then set it to 1, and increment the year by 1
   until either the year in i is > year in end_date, 
           or (year in i == year in end_date and month in i > month in end_date)

目前只是伪代码,尚未测试,但我认为沿同一行的想法会奏效.

just pseduo code for now, haven't tested, but i think the idea along the same line will work.

这篇关于查找两个日期之间月份的最​​佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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