I’m running into an issue on a clients production server where the month & day is being swapped on a view model property when saving an entity. For example if I were to select April 9th 2012 (04/09/2012) within the datepicker the date is being saved as September 4th 2012 (09/04/2012).
The same controller actions are working correctly on local development / test server and I’m unsure of what to look at next?
The property in the viewmodel:
[DisplayName("Date")]
[Required]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy}")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
The helper within the relevant view:
@Html.EditorFor(model => model.StartDate)
The jquery ui datepicker:
$('#StartDate').datepicker({ dateFormat: 'mm/dd/yy' });
The stripped down version of the controller:
public ActionResult Create(BulletinBoardViewModel bulletin)
{
var model = AutoMapper.Mapper.Map<BulletinBoardViewModel, BulletinBoard>(bulletin);
_repository.Save(model);
return RedirectToAction("Index");
}
The headers of the related post:
StartDate:04/09/2012
Cache-Control:private, s-maxage=0
Content-Length:131
Content-Type:text/html; charset=utf-8
Date:Tue, 10 Apr 2012 02:43:16 GMT
Location:/BulletinBoard
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
Sounds like the client’s server is set for British English rather than US. This is a common CultureInfo issue. Either way, when the object is persisted, the date value should still be correct. It’s just being displayed differently because of the UI culture selection.
Can you identify a particular piece of code where this is an actual issue, rather than just an apparent one?
Also, it’s not actually being saved as September 4th. You’re just reading it that way. Try picking, for example, Dec. 31st. When the entity is saved, you’ll likely see 31/12/2012. The server is just displaying the date in dd/MM/yyyy format.