title: Calculating start of day and start of month in UTC
body: <p>I have users that can be in different timezones and I'm looking to determine the UTC value of the beginning of their days and their months. Inside an object, I have a method that attempts to do that; it looks like this:</p>
  
  <pre><code>private void SetUserStartTimesUTC()
  {
      DateTime TheNow = DateTime.UtcNow.ConvertUTCTimeToUserTime(this.UserTimezoneID);
  
      DateTime TheUserStartDateUserTime = TheNow.Date;
      DateTime TheUserStartMonthUserTime = new DateTime(TheNow.Year, TheNow.Month, 1);
      DateTime TheUserEndMonthUserTime = TheUserStartMonthUserTime.AddMonths(1);
  
      this.UserStartOfDayUTC = TheUserStartDateUserTime.ConvertUserTimeToUTCTime(this.UserTimezoneID);
      this.UserStartOfMonthUTC = TheUserStartMonthUserTime.ConvertUserTimeToUTCTime(this.UserTimezoneID);
      this.UserEndOfMonthUTC = TheUserEndMonthUserTime.ConvertUserTimeToUTCTime(this.UserTimezoneID);
  }
  </code></pre>
  
  <p>And this method depends on two other extension methods that do the conversions between a user's time and UTC time</p>
  
  <pre><code>public static DateTime ConvertUserTimeToUTCTime(this DateTime TheUserTime, string TheTimezoneID)
  {
      TimeZoneInfo TheTZ = TimeZoneInfo.FindSystemTimeZoneById(TheTimezoneID);
      DateTime TheUTCTime = new DateTime();
  
      if (TheTZ != null)
      {
          DateTime UserTime = new DateTime(TheUserTime.Year, TheUserTime.Month, TheUserTime.Day, TheUserTime.Hour, TheUserTime.Minute, TheUserTime.Second);
          TheUTCTime = TimeZoneInfo.ConvertTimeToUtc(UserTime, TheTZ);
      }
  
      return TheUTCTime;
  }
  
  public static DateTime ConvertUTCTimeToUserTime(this DateTime TheUTCTime, string TheTimezoneID)
  {
      TimeZoneInfo TheTZ = TimeZoneInfo.FindSystemTimeZoneById(TheTimezoneID);
      DateTime TheUserTime = new DateTime();
  
      if (TheTZ != null)
      {
          DateTime UTCTime = new DateTime(TheUTCTime.Year, TheUTCTime.Month, TheUTCTime.Day, TheUTCTime.Hour, TheUTCTime.Minute, 0, DateTimeKind.Utc);
          TheUserTime = TimeZoneInfo.ConvertTime(UTCTime, TheTZ);
      }
  
      return TheUserTime;
  }
  </code></pre>
  
  <p>Now I've been dealing with timezone issues for a while and I know that timezone issues can introduce off-by-one bugs that can be hard to detect.</p>
  
  <p>Does my implementation of timezones seem to be correct or is there an edge case that will create some sort of off-by-one bug?</p>
  
  <p>Thanks for your suggestions.</p>