How to get the start and end times of a day

后端 未结 10 697
无人及你
无人及你 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 00:51
    public static class DateTimeExtension {        
        public static DateTime StartOfTheDay(this DateTime d) => new DateTime(d.Year, d.Month, d.Day, 0, 0,0);
        public static DateTime EndOfTheDay(this DateTime d) => new DateTime(d.Year, d.Month, d.Day, 23, 59,59);
    }
    
    0 讨论(0)
  • 2020-12-24 00:51

    In Java 8, you can do it using LocalDate as follows:

        LocalDate localDateStart = LocalDate.now();
        Date startDate = Date.from(localDateStart.atStartOfDay(ZoneId.systemDefault()).toInstant());
    
        LocalDate localDateEnd = localDateStart.plusDays(1);
        Date endDate = Date.from(localDateEnd.atStartOfDay(ZoneId.systemDefault()).toInstant());
    
    0 讨论(0)
  • 2020-12-24 00:55

    I use the following in C#

    public static DateTime GetStartOfDay(DateTime dateTime)
    {
        return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0);
    }
    public static DateTime GetEndOfDay(DateTime dateTime)
    {
        return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999);
    }
    

    Then in MS SQL I do the following:

    if datepart(ms, @dateEnd) = 0
       set @dateEnd = dateadd(ms, -3, @dateEnd)
    

    This will result in MS SQL time of 23:59:59.997 which is the max time before becoming the next day.

    You could simply use:

    new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999);
    

    Which will work in MS SQL, but this is not as accurate in .Net side.

    0 讨论(0)
  • 2020-12-24 00:57

    That's pretty much what I would do, with some small tweaks (really no big deal, just nitpicking):

    • The TryParse()/TryParseExact() methods should be used which return false instead of throwing exceptions.
    • FormatException is more specific than Exception
    • No need to check for Length == 8, because ParseExact()/TryParseExact() will do this
    • "00:00:00" and "23:59:59" are not needed
    • return true/false is you were able to parse, instead of throwing an exception (remember to check value returned from this method!)

    Code:

    private bool ValidateDatePeriod(string pdr, out DateTime startDate, 
                            out DateTime endDate)
    {
       string[] dates = pdr.Split('-');
    
       if (dates.Length != 2)
       {
           return false;
       }
    
       // no need to check for Length == 8 because the following will do it anyway
       // no need for "00:00:00" or "23:59:59" either, I prefer AddDays(1)
    
       if(!DateTime.TryParseExact(dates[0], "yyyyMMdd", null, DateTimeStyles.None, out startDate))
          return false;
    
       if(!DateTime.TryParseExact(dates[1], "yyyyMMdd", null, DateTimeStyles.None, out endDate))
          return false;
    
       endDate = endDate.AddDays(1);
       return true;
    }
    
    0 讨论(0)
  • 2020-12-24 01:03

    The DateTime object has a property called Date which will return just the date portion. (The time portion is defaulted to 12:00 am).

    I would recommend as a more elegant solution (IMHO) that if you want to allow any datetime on the last day, then you add 1 day to the date, and compare to allow times greater than or equal to the start date, but strictly less than the end date (plus 1 day).

    // Calling code.  beginDateTime and endDateTime are already set.
    // beginDateTime and endDateTime are inclusive.
    // targetDateTime is the date you want to check.
    beginDateTime = beginDateTime.Date;
    endDateTime = endDateTime.Date.AddDays(1);
    
    if ( beginDateTime <= targetDateTime &&
         targetDateTime < endDateTime )
       // Do something.
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题