How to get the start and end times of a day

后端 未结 10 699
无人及你
无人及你 2020-12-24 00:29

In my C# app, I pass a string variable that is of format yyyymmdd-yyyymmdd that represents a from and to date. I want to get the start and end times for these dates respecti

10条回答
  •  隐瞒了意图╮
    2020-12-24 01:12

    You could define two extension methods somewhere, in a utility class like so :

    public static DateTime EndOfDay(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
    }
    
    public static DateTime StartOfDay(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
    }
    

    And then use them in code like so :

    public DoSomething()
    {
        DateTime endOfThisDay = DateTime.Now.EndOfDay();
    }
    

提交回复
热议问题