How to get the start and end times of a day

后端 未结 10 741
无人及你
无人及你 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: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.
    

提交回复
热议问题