0

What i need to do here is simple but for some reason i'm just drawing a blank on this and need some assurance on how to do it correctly.

I'm trying to determine the UTC equivalent of 5AM Pacific Time tomorrow. I'm trying to do this without relying on the server time as i have no way of knowing what time zone this is.

I just need a sanity check on the following. Is this the best way to do this? Am i going to run into issues with the server time being off? Is this going to give me a accurate representation of 5am tomorrow UTC?

DateTime datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.AddDays(1).Day, 5, 0, 0); //5AM tomorrow
TimeZoneInfo pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime pstTime = TimeZoneInfo.ConvertTimeToUtc(datetime, pstZone);

TIA

8
  • use UtcNow instead of Now. Commented Jan 16, 2018 at 19:23
  • 2
    stackoverflow.com/questions/246498/… Commented Jan 16, 2018 at 19:23
  • 1
    What do you mean by tomorrow? Tomorrow in which time zone? Commented Jan 16, 2018 at 19:27
  • Your DateTime.Now values for Year, Month, and Days are all contingent on the time zone of the machine that the code is executing on. There's all sorts of wrapping that can occur trying to find "tomorrow". Commented Jan 16, 2018 at 19:28
  • @AdamBrown Tomorrow PST time Commented Jan 16, 2018 at 19:36

1 Answer 1

1

Your code is actually missing some details. Basically, the method is:

  1. Take now in UTC
  2. Convert it to pst.
  3. Take "today"
  4. Add a day to get "tomorrow"
  5. Add 5 hours to get 5am.
  6. Convert back to UTC

Code:

static void Main(string[] args)
{
     var utcNow = DateTime.UtcNow;
     TimeZoneInfo pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
     var pstNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, pstZone);

     DateTime targetPstTime = pstNow.Date.AddDays(1).AddHours(5);

     DateTime utcAnswer = TimeZoneInfo.ConvertTimeToUtc(targetPstTime, pstZone);

     Console.WriteLine(utcAnswer);
     Console.ReadKey();

 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is exactly what i needed! I appreciate you patience with me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.