Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:47:29+00:00 2026-06-02T12:47:29+00:00

I have a windows service that runs every night between 3am and 5am. When

  • 0

I have a windows service that runs every night between 3am and 5am. When the job runs the following happens:

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

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

SELECT * FROM Table WHERE CreatedAt BETWEEN startDate AND endDate

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.

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.

I tried to solve this with something like:

var now = DateTime.Now;
var midnight = DateTime.Today.ToUniversalTime();

var endDate = (now.Day == midnight.Day) ? midnight.AddDays(1) : midnight;
var startDate = endDate.AddDays(-1);

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.

Is there any clever way to use the DateTime object to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:47:31+00:00Added an answer on June 2, 2026 at 12:47 pm

    You can translate the local time to UTC in your UI; or just use DateTime.UtcNow. You have to be careful with ToUniversalTime as it only works if the DateTime you call it on has Kind==DateTimeKind.Local:

    var localTimeUtc = TimeZoneInfo.ConvertTimeToUtc(
      DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local));
    
    //or (much easier)
    var localTimeUtc = DateTime.UtcNow;
    

    And then:

    var utcMidnight = localTimeUtc.Date;
    var yesterdayUtcMidnight = midnight.AddDays(-1);
    

    now utcMidnight and yesterdayUtcMidnight will cover all of ‘yesterday’ in UTC time.

    On a second note – is your windows service meant to process data from yesterday UTC or yesterday local? If the times in the database are UTC, then your service code should work – but if they’ve gone in as local time (perhaps with a default GetDate()?) then your code for processing won’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a windows service that runs every 10 seconds to execute the read
I have a windows service that runs every 10 seconds ... each time it
There is this windows service that runs at mid night every night.For the sake
I currently have a windows service that runs every 5 minutes. The code selects
I have the following problem. I have a quartz job that runs every 5
I have a Windows service that runs as a logged-in user (local admin). During
I have a windows service that does some intensive work every one minute (actually
I'm running Windows 2003 Service Pack 2. I have a batch file that runs
I have a windows service that runs 24/7 on one of our servers. It
I have a Windows Service program running in Release mode that runs 24/7 which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.