using System; using System.Web.Http.Controllers; using System.Web.Mvc; namespace FHT.Agency.Web { public class LocalDateTimeMvcModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (value != null && value.AttemptedValue != null && !string.IsNullOrWhiteSpace(value.AttemptedValue)) { DateTime d = DateTime.Parse(value.AttemptedValue); if (d.Kind == DateTimeKind.Unspecified) { d = DateTime.SpecifyKind(d, DateTimeKind.Local); //throw new InvalidOperationException("Only accept datetime format of Local or Utc, but saw Unspecified"); } if (d.Kind == DateTimeKind.Utc) { d = d.ToLocalTime(); } return d; } return null; } } }