简体   繁体   English

查找C#中给定日期的最接近月末

[英]Find closest end-of-month for a given date in C#

I want to find the "closest" end-of-month-date to a particular date. 我想找到特定日期的“最接近”的月末日期。 For instance, if the date is 4.3.2017 , 28.2.2017 is the closest date. 例如,如果日期是4.3.201728.2.2017是最接近的日期。 For 20.3.2017 , 31.3.2017 is the closest date. 20.3.2017日, 31.3.2017月31日是最近的日期。 For dates caught dead center it doesnt really matter if we pick the lower or the higher date. 对于陷入死亡中心的日期,如果我们选择更低或更高的日期并不重要。

From these two posts, How do I get the last day of a month? 从这两个帖子中, 我如何获得一个月的最后一天? and Find the closest time from a list of times , I have been able to clobber together the following approach 并且从列表中找到最接近的时间 ,我已经能够将以下方法拼凑在一起

public static DateTime findNearestDate(DateTime currDate)
{
    List<DateTime> dates = new List<DateTime> { ConvertToLastDayOfMonth(currDate.AddMonths(-1)), ConvertToLastDayOfMonth(currDate) };
    DateTime closestDate = dates[0];
    long min = long.MaxValue;

    foreach (DateTime date in dates)
        if (Math.Abs(date.Ticks - currDate.Ticks) < min)
        {
            min = Math.Abs(date.Ticks - currDate.Ticks);
            closestDate = date;
        }
    return closestDate;
}

public static DateTime ConvertToLastDayOfMonth(DateTime date)
{
    return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
}

This works, but it seems like there is a lot of code for such a simple task. 这很有效,但似乎有很多代码可以完成这么简单的任务。 Does anybody know of a simpler, more compact, approach? 有人知道更简单,更紧凑的方法吗?

Given that there can only be two options, it seems odd to loop here. 鉴于只能有两个选项,这里循环似乎很奇怪。

Assuming you only have dates, rather than needing to worry about the time of day as well, it seems to me that the decision only rests on how many days are in the "current" month. 假设你只有日期,而不需要担心一天中的时间,在我看来,决定只取决于“当前”月份中的天数。 So something like: 所以类似于:

// Names adjusted to follow .NET naming conventions
public static DateTime FindNearestEndOfMonth(DateTime date)
{
    int year = date.Year;
    int month = date.Month;
    int daysInMonth = DateTime.DaysInMonth(year, month);
    return date.Day >= daysInMonth / 2
        // End of current month
        ? new DateTime(year, month, daysInMonth)
        // End of previous month
        : new DateTime(year, month, 1).AddDays(-1);
}

You can calculate the last date of current and previous month and choose the closest one: 您可以计算当前和上个月的最后日期,并选择最近的日期:

public static DateTime GetNearestEOM(DateTime date)
{
    DateTime EOMPrev = new DateTime(date.Year, date.Month, 1).AddDays(-1);
    DateTime EOMNext = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
    DateTime NearestEOM = (date - EOMPrev).TotalDays < (EOMNext - date).TotalDays ? EOMPrev : EOMNext;
    return NearestEOM;
}

GetNearestEOM(new DateTime(2017, 3, 4));  // 2017-02-28 00:00:00
GetNearestEOM(new DateTime(2017, 3, 20)); // 2017-03-31 00:00:00

There is no need for a loop. 不需要循环。 You can use the built-in timespan addition functions of the framework: 您可以使用框架的内置时间跨度添加功能:

var closestendofmonth = new DateTime(date.Year, date.Month, 1).AddDays(-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM