获取具体日期

时间:2013-06-16 12:00:23

标签: c# date datetime

我正在使用C#中的日期,需要计算更多日期。取当前datetime

如何获得以下值?

  • 一天结束
  • 月底
  • 年末

3 个答案:

答案 0 :(得分:9)

如果当天end of ...表示12:59:59.999PM,那么:

  • 一天结束

    var today = DateTime.Today;
    var endOfDay = new DateTime(
        today.Year, 
        today.Month,
        today.Day,
        23,
        59,
        59,
        999
    );
    
  • 月底

    var today = DateTime.Today;
    var endOfMonth = new DateTime(
        today.Year, 
        today.Month,
        DateTime.DaysInMonth(today.Year, today.Month),
        23,
        59,
        59,
        999
    );
    
  • 年末

    var today = DateTime.Today;
    var endOfYear = new DateTime(
        today.Year,
        12,
        31,
        23,
        59,
        59,
        999
    );
    

如果你的意思是别的,那就解释一下你的意思。

答案 1 :(得分:2)

月末

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));

请参阅How can I get the last day of the month in C#?

年末

DateTime endOfYear = new DateTime(today.Year, 12, 31);

对于一天结束,假设一天结束时间下午5点结束

DateTime endOfDay = new DateTime(today.Year, today.Month, today.day, 17, 0, 0); // Assuming the business end of day is at 5 PM

http://msdn.microsoft.com/en-us/library/system.datetime.aspx

查看有关日期时间的详情

答案 2 :(得分:0)

每月的最后一天:

DateTime today = DateTime.Now;
DateTime lastDayOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));

一年的最后一天:

DateTime today = DateTime.Now;
DateTime lastDayOfYear = new DateTime(today.Year, 12, 31);