I am using daterangepicker. In my form, I have two hidden fields:
@Html.HiddenFor(x=> x.Header.StartDate, new {id="startdate"})
@Html.HiddenFor(x => x.Header.EndDate, new { id = "enddate" })
I also have the input which is for the dates to be selected from:
<input class="input-sm form-control" placeholder="Enter your dates" type="text" name="daterange" value="" />
When the user has selected an end date, the two hidden fields are populated with the start and end date, respectively.
The daterangepicker is managed via JS:
$(function () {
$('input[name="daterange"]').daterangepicker({
locale: {
format: 'DD/MM/YYYY'
},
"autoApply": true
});
$('input[name="daterange"]').val('');
$('input[name="daterange"]').on('apply.daterangepicker', function (ev, picker) {
var startdate = picker.startDate.format('DD/MM/YYYY');
var enddate = picker.endDate.format('DD/MM/YYYY');
$("#startdate").val(startdate);
$("#enddate").val(enddate);
});
});
My HeaderViewModel looks like this (setting the format makes no difference):
public class HeaderViewModel
{
//[DataType(DataType.Date), DisplayFormat(DataFormatString = "{dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? StartDate { get; set; }
// [DataType(DataType.Date), DisplayFormat(DataFormatString = "{dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? EndDate { get; set; }
}
If I choose a date with a day less than 12, eg: 11 March 2015, it works. However, if I select a date, say, 23 March 2015, it will not work because the date is invalid... it is expecting a US format.
I am using SQL server 2012 and have set the language to British English.
Anyone have any ideas? It's killing me.