Incorrect date format when using DatePicker

0

I have a DateTime field with its BootstrapDatePicker like the following:

 <div class='input-group date' id='datetimepicker1'>
       @Html.TextBoxFor(m => m.DatePicker, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.DatePicker)                  
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
       </span>
         </div>

And its respective Javascript:

 $(document).ready(function () {
            //DatePicker
            $('#datetimepicker1').datetimepicker({
                defaultDate: new Date(),
                format: 'MM/DD/YYYY',
                minDate: new Date(),
                locale: 'es'
            });

Model:

[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd.MM.yy}", ApplyFormatInEditMode = true)]
        [Required(ErrorMessage = "Formato incorrecto")]
        public DateTime? DatePicker { get; set; }

The problem is sending the date. I accept values from 03/1/2018 to 03/12/2018 works correctly, but if I send 03/13/2018 the page only returns a BadRequest. The format of my date is in English format. I do not understand why it is failing to send you the value of 13 onwards. I hope you can help me. Thanks

    
asked by David 07.03.2018 в 02:01
source

3 answers

0

You can handle formats to your liking on your dates both on the backend and frontend side. But when working on the date as such, it is best to keep your Datetime type. With datepicker, it uses its function of getDate .

let fecha = $('#datetimepicker1').datepicker("getDate");

If you still use this function if you still have the dates changed, then apply .toISOString () to your Datetime; You can even call it on the same line

let fecha = $('#datetimepicker1').datepicker("getDate").toISOString()
    
answered by 08.03.2018 / 00:59
source
0

In fact, the formats are different, in the ordering of the picker format: 'MM/DD/YYYY', and your model DataFormatString = "{0:dd.MM.yy}" as you see in the picker you give an order mes/día/año and in the model día/mes/año this is the problem, since that, they must be the same. If what you are doing is intended for people in Latin America, I recommend that you only change the picker by format: 'DD/MM/YYYY', . With this there should no longer be problems.

    
answered by 07.03.2018 в 16:10
0

Why do not you do it this way much easier?

HTML

        <input type="text" id="TXTFecha" style="margin-top: 0px; width: 81.39%" class="form-control" />

Jquery

    $(function () {

    $("#TXTFecha").datepicker({
        dateFormat: 'yy/mm/dd',//Puedes poner el formato que deseas(dd.mmm.yy | mm/dd/yy | dd-mm-yy ETC)
        onSelect: function (dateText, inst) {
            finicio = dateText; //Ésta variable la puedes enviar a tu controlador siempre con el valor actual.
        }
    });

    });

I hope you serve, greetings.

    
answered by 07.03.2018 в 23:49