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

Nugs
  • 5,503
  • 7
  • 36
  • 57

1 Answers1

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();

 }
Adam Brown
  • 1,667
  • 7
  • 9