月底计算
只是想知道是否有人知道以下问题的优雅解决方案。
如果我有 2009 年 6 月 30 日,并且我添加了一个月,我希望它转到 2009 年 7 月 31 日,而不是 2009 年 7 月 30 日。
这个逻辑基于以下事实:2009 年 6 月 30 日是 6 月的月底,当我添加一个月我想去下个月月底。
但是,如果我有 2009 年 6 月 29 日,并且添加一个月,则应该到 2009 年 7 月 29 日。
请注意,我需要能够添加任意数量的月份,并且需要考虑闰年。
我也知道这里的逻辑是有问题的,但这是一项业务要求,适用于未来一个月的月末合同。
我想到了几种解决方案,但没有一个是非常优雅的。 因此我想有人可能有更好的方法。
干杯 安东尼
Just wondering if any know of an elegant solution for the following.
If I have 30 June 2009 and I add a month I want it to go to 31 July 2009, not the 30 July 2009.
This logic is based on the fact that the 30 June 2009 was the end of the month of June and when I add a month I want to go to the end of the next month.
But if I have 29 June 2009 and I add a month it should go to 29 July 2009.
Note I need to be able to add any number of months and I need to take into account leap years.
Also I know the logic here is questionable but it is a business requirement that works with end of month contracts going to the end of the month for a month in the future.
I have thought of several solution but none that are very elegant. Hence I was thinking someone might have a better way.
Cheers
Anthony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要检查某个日期是否是月底,请检查第二天是否是某个月的第一天。 那么你的算法应该是“如果这一天不是月底,则添加 1 个月。如果是月底,则添加 1 天,添加 1 个月,减去 1 天。”
To check if a date is the end of the month, you check if the next day is day one of some month. your algorithm should then be "If the day is not end of month, add 1 month. If it is the end of the month, add one day, add one month, subtract one day."
编辑:我再次阅读了你的问题。 您需要检查是否是该月的最后一天。
抱歉,我希望我已经清楚地阅读了需要什么的问题。
希望这足以让您了解需要什么。
EDIT: I read your question again. You will need to put a check to see if its last day of the month.
Sorry, I wish I had read the question clearly of what is needed.
Hope this is good enough to give you an idea of what is needed.
从上个月的第一天开始,一天返回可以到达那里吗?
Can you get there by starting at the first day of the previous month and going back one day?
我解决这个问题的方法首先是建立一个例程来确定给定的一天是否是该月的最后一天(当然要考虑到闰年二月!)
您还需要一个例程来为您提供某个月的最后一天月,给定该月的某一天。
然后,如果您遇到最后一天,请添加一天,并使用第二个例程获取该月的最后一天。 使用循环数月。
My approach to this problem would begin with having a routine that determines if a given day is the last day of the month (taking into account leap year Februaries of course!)
You'll also need a routine that gives you the last day of a month, given a day in that month.
Then, if you encounter a day that IS a last day, add one day, and use the second routine to get the last day of THAT month. Use a loop for multiple months.
您可以实现 EndOfMonth() 和 isEndOfMonth()。
所以你的代码或多或少会有点
简单,但你明白了。
当然,对于 EndOFMonth 和 isEndOfMonth 的逻辑,这里有很多想法
You could implement a EndOfMonth() and isEndOfMonth().
so your code would be, more or less,
A bit simplistic but you get the idea.
There is of course alot of ideas here for the logic of EndOFMonth and isEndOfMonth
这是一个简单的 .NET C# 解决方案,用于计算闰年等:
Here is a simple .NET C# solution that counts for leap years, etc:
您可以使用 DateTime.DaysInMonth() 方法,如下所示:
You could use the DateTime.DaysInMonth() method something like the following:
那么这是否会成为查找我是否处于月初的补充函数?
<代码>
DateTime.Now.AddDays(-1).Month == DateTime.Now.AddMonths(-1).Month
Would this then be the complement function to find if I am on the Start of a Month?
DateTime.Now.AddDays(-1).Month == DateTime.Now.AddMonths(-1).Month
如果您只需要 EndOfMonth 或简单的 AddNMonth,那么您已经得到答案了。
否则,要拥有完整的通用解决方案,您需要实现重复模式,正如您在 Outlook 会议界面。 我并不是建议您使用 Outlook 实现,但您应该可以购买一些 .NET 组件。 或者自己实现它,但不要低估它并对其进行单元测试 - 这是一个实现起来非常棘手的组件。
If you need only EndOfMonth or simple AddNMonth, then you have got your answer already.
Otherwise, to have a complete generic solution you would need an implementation of the Recurrence pattern as you may see it in the UI of the Outlook Meeting interface. I am not suggesting you to use Outlook implementation though, but there should be a few .NET components that you can buy. Or implement it yourself, but do not underestimate and unit-test it all - it is a very tricky component to implement.