Previous days count using DateTime causes issues in UTC format
<p>I have a windows service that runs every night between 3am and 5am. When the job runs the following happens:</p>

<pre><code>var endDate = DateTime.Today.ToUniversalTime(); // Set to midnight
var startDate = endDate.AddDays(-1);
</code></pre>

<p>The query that runs at night says something like (All dates are currently stored as UTC):</p>

<pre><code>SELECT * FROM Table WHERE CreatedAt BETWEEN startDate AND endDate
</code></pre>

<p>This works just fine, it basically grabs all the data from the previous day when the job runs. I am now developing a UI part to this where a user clicks a button to see the data count that will be processed at night.</p>

<p>The problem I am running into is when a user is at work during normal business hours and clicks that button the counts will be off by 1 day. The count will only display correctly if the local time is after 8pm EST (server is located on the east coast) since that will be after midnight in UTC time.</p>

<p>I tried to solve this with something like:</p>

<pre><code>var now = DateTime.Now;
var midnight = DateTime.Today.ToUniversalTime();

var endDate = (now.Day == midnight.Day) ? midnight.AddDays(1) : midnight;
var startDate = endDate.AddDays(-1);
</code></pre>

<p>But this is incorrect as it will only work during some parts of the day. If the button is clicked after midnight local time it would be off by one day again.</p>

<p>Is there any clever way to use the DateTime object to solve this problem?</p>
