title: NodaTime Invalid DateTime.Kind for Instant.FromDateTimeUtc
body: <p>I'm trying to get ahold of this timezone issue we are having. We would like to store all <code>DateTime</code>s in UTC, and then convert the <code>DateTime</code> to the user's timezone. </p>
  
  <p>We decided to use NodaTime for this, as it seems like the right approach. However, we are experiencing an issue with it.</p>
  
  <p>This is how we convert the <code>DateTime</code> to UTC (note - I hardcoded the <code>usersTimeZone</code> for now):</p>
  
  <pre><code>public static DateTime ConvertLocaltoUTC(this DateTime dateTime)
  {
      LocalDateTime localDateTime = LocalDateTime.FromDateTime(dateTime);
  
      IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
      var usersTimezoneId = "Europe/Copenhagen";
      var usersTimezone = timeZoneProvider[usersTimezoneId];
  
      var zonedDbDateTime = usersTimezone.AtLeniently(localDateTime);
      var returnThis = zonedDbDateTime.ToDateTimeUtc();
      return zonedDbDateTime.ToDateTimeUtc();
  }
  </code></pre>
  
  <p>And here is how we convert it back:</p>
  
  <pre><code>public static DateTime ConvertUTCtoLocal(this DateTime dateTime)
  {
      Instant instant = Instant.FromDateTimeUtc(dateTime);
      IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
      var usersTimezoneId = "Europe/Copenhagen"; //just an example
      var usersTimezone = timeZoneProvider[usersTimezoneId];
      var usersZonedDateTime = instant.InZone(usersTimezone);
      return usersZonedDateTime.ToDateTimeUnspecified();
  }
  </code></pre>
  
  <p>However, when we convert it back to local time, it throws an exception:</p>
  
  <blockquote>
    <p>Argument Exception: Invalid DateTime.Kind for Instant.FromDateTimeUtc</p>
  </blockquote>
  
  <p>at the first line of <code>ConvertUTCtoLocal()</code>.</p>
  
  <p>An example of the <code>DateTime</code> could be: "9/18/2017 5:28:46 PM" - yes this has been through the <code>ConvertLocalToUTC</code> method.</p>
  
  <p>Am I providing an incorrect format? What am I doing wrong here?</p>