月底计算

发布于 2024-07-24 06:03:29 字数 398 浏览 3 评论 0原文

只是想知道是否有人知道以下问题的优雅解决方案。

如果我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

故人的歌 2024-07-31 06:03:29

要检查某个日期是否是月底,请检查第二天是否是某个月的第一天。 那么你的算法应该是“如果这一天不是月底,则添加 1 个月。如果是月底,则添加 1 天,添加 1 个月,减去 1 天。”

    bool IsEndOfMonth(DateTime date) {
        return date.AddDays(1).Day == 1;
    }
    DateTime AddMonthSpecial(DateTime date) {
        if (IsEndOfMonth(date))
            return date.AddDays(1).AddMonths(1).AddDays(-1);
        else
            return date.AddMonths(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."

    bool IsEndOfMonth(DateTime date) {
        return date.AddDays(1).Day == 1;
    }
    DateTime AddMonthSpecial(DateTime date) {
        if (IsEndOfMonth(date))
            return date.AddDays(1).AddMonths(1).AddDays(-1);
        else
            return date.AddMonths(1);
    }
沫离伤花 2024-07-31 06:03:29
DateTime exampleDate = DateTime.Parse("12/31/2009");
bool isLastDayOfMonth = (exampleDate.AddDays(1).Month != exampleDate.Month);

if (isLastDayOfMonth)
    Console.WriteLine(exampleDate.AddDays(1).AddMonths(1).AddDays(-1));
else
    Console.WriteLine(new DateTime(exampleDate.Year, exampleDate.Month, 1).AddMonths(2).AddDays(-1));

编辑:我再次阅读了你的问题。 您需要检查是否是该月的最后一天。

抱歉,我希望我已经清楚地阅读了需要什么的问题。
希望这足以让您了解需要什么。

DateTime exampleDate = DateTime.Parse("12/31/2009");
bool isLastDayOfMonth = (exampleDate.AddDays(1).Month != exampleDate.Month);

if (isLastDayOfMonth)
    Console.WriteLine(exampleDate.AddDays(1).AddMonths(1).AddDays(-1));
else
    Console.WriteLine(new DateTime(exampleDate.Year, exampleDate.Month, 1).AddMonths(2).AddDays(-1));

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.

秉烛思 2024-07-31 06:03:29

从上个月的第一天开始,一天返回可以到达那里吗?

Can you get there by starting at the first day of the previous month and going back one day?

  1. 测试旧日期是否为“月底”。
    1. 在日期中添加一天,看看月份数字是否发生变化。
  2. 添加月份
  3. 如果旧日期是“月底”
    1. 将月份中的某一天设置为 1
    2. 再添加一个月
    3. 减去一天
  1. Test to see if the old date is "end-of-month".
    1. Add a day to the date and see if the month number changes.
  2. Add a month
  3. If the old date was "end-of-month"
    1. Set the day of month to 1
    2. Add another month
    3. Subtract one day
长梦不多时 2024-07-31 06:03:29

我解决这个问题的方法首先是建立一个例程来确定给定的一天是否是该月的最后一天(当然要考虑到闰年二月!)

您还需要一个例程来为您提供某个月的最后一天月,给定该月的某一天。

然后,如果您遇到最后一天,请添加一天,并使用第二个例程获取该月的最后一天。 使用循环数月。

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.

街角迷惘 2024-07-31 06:03:29

您可以实现 EndOfMonth() 和 isEndOfMonth()。

所以你的代码或多或少会有点

if isEndOfMonth( this.Date() )
  endDate = (startmonth + addedMonths).EndOfMonth()
else
  endDate = startDate + addedMonths

简单,但你明白了。

当然,对于 EndOFMonth 和 isEndOfMonth 的逻辑,这里有很多想法

You could implement a EndOfMonth() and isEndOfMonth().

so your code would be, more or less,

if isEndOfMonth( this.Date() )
  endDate = (startmonth + addedMonths).EndOfMonth()
else
  endDate = startDate + addedMonths

A bit simplistic but you get the idea.

There is of course alot of ideas here for the logic of EndOFMonth and isEndOfMonth

静若繁花 2024-07-31 06:03:29

这是一个简单的 .NET C# 解决方案,用于计算闰年等:

static DateTime ContractDue(DateTime start, int months)
{
     if (start.Month == start.AddDays(1).Month)
     { // Same month, just add the months
            return start.AddMonths(months);
     }
     // Last day of month... add a day, add the months, then go back one day
     return start.AddDays(1).AddMonths(months).AddDays(-1);
}

Here is a simple .NET C# solution that counts for leap years, etc:

static DateTime ContractDue(DateTime start, int months)
{
     if (start.Month == start.AddDays(1).Month)
     { // Same month, just add the months
            return start.AddMonths(months);
     }
     // Last day of month... add a day, add the months, then go back one day
     return start.AddDays(1).AddMonths(months).AddDays(-1);
}
不可一世的女人 2024-07-31 06:03:29

您可以使用 DateTime.DaysInMonth() 方法,如下所示:

DateTime workingMonth = new DateTime(2009, 06, 30).AddDays(1);
int nextMonthDays = DateTime.DaysInMonth(workingMonth.Year, workingMonth.Month);
DateTime newMonth = new DateTime(workingMonth.Year, workingMonth.Month, nextMonthDays);

You could use the DateTime.DaysInMonth() method something like the following:

DateTime workingMonth = new DateTime(2009, 06, 30).AddDays(1);
int nextMonthDays = DateTime.DaysInMonth(workingMonth.Year, workingMonth.Month);
DateTime newMonth = new DateTime(workingMonth.Year, workingMonth.Month, nextMonthDays);
许仙没带伞 2024-07-31 06:03:29

那么这是否会成为查找我是否处于月初的补充函数?
<代码>
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

日裸衫吸 2024-07-31 06:03:29

如果您只需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文