How to get the start and end times of a day

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

    If you are only worried about .Net precision...

    startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
    endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddTicks(-1).AddDays(1);
    

    You really don't need to concatenate extra values onto the string for the time portion.


    As an addendum, if you are using this for a query against, for example, a database...

    startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
    endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddDays(1);
    

    With a query of...

    WHERE "startDate" >= @startDate AND "endDate" < @endDate
    

    Then the precision issues noted in the comments won't really matter. The endDate in this case would not be part of the range, but the outside boundary.

提交回复
热议问题