Change date format to Brazilian default, datepicker JQuery

1

I'm having a problem with date format, if I put dateFormat: 'dd/mm/yy , my alert returns me invalid date, if I pull it off and leave American default then returns the date. Any ideas how to resolve this?

I want to keep the Brazilian standard date, but not only with a mask but the format itself.

Follow the example in JSFiddle

<div id="teste">
  <label class="fa fa-calendar">
    <input id="startDate" name="start_at" placeholder=" " class="data-charts" type="text" value="">
  </label>
  <label class="fa fa-calendar">
    <input id="endDate" name="end_at" placeholder=" " class="data-charts" type="text" value="">
  </label>
  <button type="button" class="btn btn-apply">Aplicar</button>
</div>
$(document).ready(function() {
    $("#startDate").datepicker({
        numberOfMonths: 1,
        onSelect: function(selected) {
            $("#endDate").datepicker("option","minDate", selected)
        }
    }).datepicker("setDate", "-7", new Date());

    $("#endDate").datepicker({
        numberOfMonths: 1,
        onSelect: function(selected) {
             $("#startDate").datepicker("option","maxDate", selected)
        }
    }).datepicker("setDate", new Date());


    function dateInterval() {
        var data_inicial = document.getElementById("startDate").value;
        var data_final = document.getElementById("endDate").value;

        var date1 = new Date(data_inicial);
        var date2 = new Date(data_final);
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
            if(diffDays <= 30)
            {
                alert(date1);
            }else
            {
                alert(date1);
            }
    };

    $(".btn-apply").on('click',function(){
        dateInterval();
    });

});
    
asked by anonymous 07.04.2017 / 19:57

1 answer

2

Here's a solution, I just needed to change your .js

$(document).ready(function() {
    $("#startDate").datepicker({
        numberOfMonths: 1,
    dateFormat: "dd/mm/yy",
        onSelect: function(selected) {
            $("#endDate")
      .datepicker("option","minDate", selected)
        }
    }).datepicker("setDate", "-7", new Date());

    $("#endDate").datepicker({
        numberOfMonths: 1,
    dateFormat: "dd/mm/yy",
        onSelect: function(selected) {
             $("#startDate")
       .datepicker("option","maxDate", selected)
        }
    }).datepicker("setDate", new Date());


    function dateInterval() {
        var data_inicial = document.getElementById("startDate").value.split("/");
    var data_final = document.getElementById("endDate").value.split("/");

        var date1 = new Date(data_inicial[2], data_inicial[1] - 1, data_inicial[0]);
        var date2 = new Date(data_final[2], data_final[1] - 1, data_final[0]);
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
            if(diffDays <= 30)
            {
                alert(date1);
            }else
            {
                alert(date1);
            }
    };

    $(".btn-apply").on('click',function(){
        dateInterval();
    });

});

link

    
07.04.2017 / 20:13