What is the preferred practice to handle DateTime format between client (javascript, ajax) and server (ASP MVC) for an international application?
Based on my research:
- Server Format: yyyy-mm-dd
- Client Format: yyyy-mm-dd
Overwrite the DateTime model binder of ASP MVC with custom model binder like
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
try
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return value.ConvertTo(typeof(DateTime), CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
return new DateTime();
}
}
and format the date at client side by:
function toISOString(d) {
var year = d.getFullYear();
var month = d.getMonth() + 1;
var date = d.getDate();
return year + '-' + month + '-' + date;
}
and one last question - having set the above, how the server check the DateTime offset or Timezone offset of the client if that has to take in to account before going into the application?