title: Handling time-zone between client and server for date-only DateTime
body: <p>I have a custom validation attribute in my ASP.NET Web API application that checks if the date is in the future or not (dates in the future are not allowed). Here's the code:</p>
  
  <pre><code>protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  {
      if (value != null)
      {
          var date = (DateTime)value;
          if (date != null)
          {
              if(date.Date &gt; DateTime.UtcNow.Date)
              {
                  return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
              }
          }
      }
  
      return ValidationResult.Success;
  }
  </code></pre>
  
  <p>I can't figure out how to make the comparison so it works for all time-zones. Using <code>DateTime.UtcNow</code> is not the solution, because if the client and the server are in the same timezone, in the hours close to midnight, the date will be the day after. And, of course <code>DateTime.Now</code> wouldn't work for other time-zones. So, what's the solution?  </p>
  
  <p><strong>Update:</strong></p>
  
  <p>In my <code>WebApiConfig.cs</code> file, I have this code to set the <code>DateTimeZoneHandling</code> to <code>Utc</code>:</p>
  
  <pre><code>jsonFormatter.SerializerSettings = new JsonSerializerSettings
  {
      ContractResolver = new CamelCasePropertyNamesContractResolver(),
      DateFormatHandling = DateFormatHandling.IsoDateFormat,
      DateTimeZoneHandling = DateTimeZoneHandling.Utc
  };
  </code></pre>
  
  <p>This will format the date in the JSON response like this: "2018-03-02T00:00:00Z".</p>
  
  <p>And, all the DateTime values coming from the client will have their <code>Kind</code> property set to <code>Utc</code>. Then, I can compare the date with <code>UtcNow</code>, but now the problem is the date "2018-03-02T00:00:00Z" is displayed as March 1, 2018 in my browser, because it is converted to local time (UTC -5).</p>