Skip to content

Bind a JSON date value

Bind a request value to a DateTime, tolerating missing or unparseable input. Trigger: MVC model binding for a bound DateTime when the binder is registered.

Field Value
Operation ID OP-18
Trigger MVC model binding for a bound DateTime when the binder is registered
Inputs bindingContext.ValueProvider value for ModelName
Outputs DateTime, or the raw null value
Primary components JsonDateTimeModelBinder.BindModel(ControllerContext, ModelBindingContext)
Entry point src/Web.Mvc/ModelBinders/JsonDateTimeModelBinder.cs

Steps

  1. When no value is found, return new DateTime() (0001-01-01) (BR-39).
  2. When value.RawValue is null, return value.RawValue (null).
  3. When the attempted value starts with /, treat it as the ASP.NET JSON date form /Date(ms)/: take the substring from index 6, strip the trailing )/, add that many milliseconds to the Unix epoch 1970-01-01, and convert with ToLocalTime() (BR-40).
  4. Otherwise call DateTime.TryParse; on failure the out value remains default(DateTime) and is returned, so a parse failure never raises a binding error (BR-41).

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start([Model binding])
    NoVal{Value found?}
    Default([Return DateTime default])
    Null{RawValue null?}
    ReturnNull([Return null])
    Slash{Starts with /?}
    JsonDate["Parse /Date(ms)/ epoch form"]
    TryParse[DateTime.TryParse]
    EndOk([Return DateTime])
    Start --> NoVal
    NoVal -->|No| Default
    NoVal -->|Yes| Null
    Null -->|Yes| ReturnNull
    Null -->|No| Slash
    Slash -->|Yes| JsonDate
    Slash -->|No| TryParse
    JsonDate --> EndOk
    TryParse --> EndOk

Failure and edge-case behavior

  • No unexpected failure paths beyond argument-guard exceptions and delegated dependency behavior were identified for this operation.

Sources

  • src/Web.Mvc/ModelBinders/JsonDateTimeModelBinder.cs