1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pookie
  • 3,796
  • 6
  • 49
  • 105
  • How have you set the language? –  Sep 20 '15 at 10:51
  • @StephenMuecke I set the language via SQL Management Studio. It was previously the default US format. – pookie Sep 20 '15 at 10:53
  • 1
    Take a look at this question: http://stackoverflow.com/questions/528545/mvc-datetime-binding-with-incorrect-date-format - there's a variety of different solutions. I personally feel, though, that creating your own binder is the best option. http://stackoverflow.com/a/11903896/563532 – Rob Sep 20 '15 at 10:54
  • @pookie setting your current user account's language settings won't make a difference if your application isn't running under your account. What happens if you render `CultureInfo.CurrentCulture.DateTimeFormatInfo.ShortDateFormat` ? – Dai Sep 20 '15 at 10:55
  • An alternative would be to have `EndDate` defined as a string, and have a get-only property which returns the parsed value. – Rob Sep 20 '15 at 10:56
  • 1
    `SQL Management Studio` has got nothing to do with it. You need to set the culture in the `web.config` file. In `` add `` –  Sep 20 '15 at 10:57
  • @Rob Thank you! I saw a response there that suggested adding to my web.config and it now works. – pookie Sep 20 '15 at 10:58
  • Thank you everyone for your help. – pookie Sep 20 '15 at 11:00

0 Answers0