Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 你觉得这个月的最后一天怎么样?_C#_Datetime - Fatal编程技术网

C# 你觉得这个月的最后一天怎么样?

C# 你觉得这个月的最后一天怎么样?,c#,datetime,C#,Datetime,可能重复: 到目前为止,我有: DateTime createDate = new DateTime(year, month, 1).AddMonths(1).AddDays(-1); 有更好的方法吗?使用: (请注意-必须在…)中简化此操作。您可以使用方法DateTime.DaysInMonth(year,month)获取任何给定月份的天数。如果您对自定义代码版本感兴趣: var anyDt = DateTime.Now; var lastDayOfMonth = anyDt.AddMon

可能重复:

到目前为止,我有:

DateTime createDate = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
有更好的方法吗?

使用:


(请注意-必须在…)中简化此操作。

您可以使用方法
DateTime.DaysInMonth(year,month)
获取任何给定月份的天数。

如果您对自定义代码版本感兴趣:

var anyDt = DateTime.Now;
var lastDayOfMonth = anyDt.AddMonths(1).AddDays(anyDt.AddMonths(1).Day).Date;
或:

或者作为一种方法:

DateTime LastDayInMonth(DateTime anyDt)
   { return anyDt.AddMonths(1).AddDays(anyDt.AddMonths(1).Day).Date; }
或作为扩展方法:

DateTime LastDayInMonth(DateTime this anyDt)
   { return anyDt.AddMonths(1).AddDays(anyDt.AddMonths(1).Day).Date; }

我在CodePlex上的一个有用的DateTime扩展库中找到了一种优雅的方法:

下面是一些示例代码:

    public static DateTime First(this DateTime current)
    {
        DateTime first = current.AddDays(1 - current.Day);
        return first;
    }

    public static DateTime First(this DateTime current, DayOfWeek dayOfWeek)
    {
        DateTime first = current.First();

        if (first.DayOfWeek != dayOfWeek)
        {
            first = first.Next(dayOfWeek);
        }

        return first;
    }

    public static DateTime Last(this DateTime current)
    {
        int daysInMonth = DateTime.DaysInMonth(current.Year, current.Month);

        DateTime last = current.First().AddDays(daysInMonth - 1);
        return last;
    }

它还有一些其他有用的扩展,可能会对您有所帮助。

您以前是否编写过自己的日历?相比之下,这是一个极好的解决方案DJust回顾这篇文章,这怎么可能是重复的?我的问题提出了一种方法,然后询问是否有更好的方法。引用的一个问题只是询问任何方法。我讨厌人们在没有引用另一个问题的情况下将问题标记为重复问题。如果你认为还有更好的问题和答案,我也希望看到。@JonSkeet你在NodaTime中有没有让这件事变得简单过,或者这仍然是最好的方式?@BenJenkinson:这绝对是目前最好的方式。在2.0中,你可以使用一个日期调整器,目前还没有一个用于月的最后一天,但我今晚可能会添加一个:)@BenJenkinson:Ha-原来我已经做了日期调整器,在NodaTime2.0问世之前,我一直将该方法的内部部分用作自己的扩展方法。在我的例子中,我还想找到“指定日期的最后一个可能时刻”,这个方法听起来合理吗
anyLocalDate.PlusDays(1).AtMidnight().PlusTicks(-1)
@César:调用
DateTime真是个糟糕的主意。今天,像这样多次调用
。如果你在午夜左右叫醒它,它会在中途给出不同的结果。编写一个接受单个
DateTime
并一致使用的方法要好得多。此代码最多返回下个月的第一天,而不是当前月的最后一天。此外,它在处理不同天数的月份时也会遇到问题。如果今天是2017年1月4日,此代码将返回2017年2月1日。如果今天是2017年1月31日,那么这段代码将返回2017年1月29日。我知道这是一篇老帖子——今天在它上面发生了,codeplex有点死了,但我想指出的是,您的第二个First()使用了另一个扩展,您在代码中没有提供,这就是下一个函数:public static DateTime Next(此DateTime当前,DayOfWeek DayOfWeek){int offsetDays=DayOfWeek-current.DayOfWeek;如果(offsetDays
DateTime LastDayInMonth(DateTime this anyDt)
   { return anyDt.AddMonths(1).AddDays(anyDt.AddMonths(1).Day).Date; }
    public static DateTime First(this DateTime current)
    {
        DateTime first = current.AddDays(1 - current.Day);
        return first;
    }

    public static DateTime First(this DateTime current, DayOfWeek dayOfWeek)
    {
        DateTime first = current.First();

        if (first.DayOfWeek != dayOfWeek)
        {
            first = first.Next(dayOfWeek);
        }

        return first;
    }

    public static DateTime Last(this DateTime current)
    {
        int daysInMonth = DateTime.DaysInMonth(current.Year, current.Month);

        DateTime last = current.First().AddDays(daysInMonth - 1);
        return last;
    }