Chcę pokazać wszystkie daty między 2 datami. Więc napisałem,

$( document ).ready(function() {

          $( "#day" ).click(function() {

                 startJsonSession();
                  return false;
            });

            function  startJsonSession(){

            var inputdate=$('#inputdate').val();
            //alert("Input Date!!!" + inputdate );

                var d = new Date(inputdate);

                var nowMS = d.getTime(); // get # milliseconds for today
                //alert(nowMS);
                var week = 1000*60*60*24*7; // milliseconds in one week
                //alert(week);
                var oneWeekFromNow = new Date(nowMS + week);
                //alert("oneWeekFromNow!!!" + oneWeekFromNow);
                var fromdate = d.getDate();

                var month = d.getMonth() + 1;
                var year = d.getFullYear();
                if (fromdate < 10) {
                    fromdate = "0" + fromdate;
                }
                if (month < 10) {
                    month = "0" + month;
                }
                //var date = fromdate + "/" + month + "/" + year;
                var date = year + "/" + month + "/" + fromdate;
                alert("InputDate!!!!" + date);

                //var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
                var weekdate=year + "/" + month + "/" + oneWeekFromNow.getDate();
                alert("weekdate!!!" + weekdate);


                var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));

                var tomorrowdate = tomorrow.getDate();

                var month1 = tomorrow.getMonth() + 1;
                var year1 = tomorrow.getFullYear();
                if (tomorrowdate < 10) {
                    tomorrowdate = "0" + tomorrowdate;
                }
                if (month1 < 10) {
                    month1 = "0" + month1;
                }

                //var nextday = tomorrowdate + "/" + month1 + "/" + year1;
                var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
                alert("tomorrow!!!!" + nextday);


                    var d1 = new Date(date);
                    alert("D1!!!!!" + d1);
                    var d2 = new Date(weekdate);
                    var aDates = [];

                    do
                    {
                    aDates.push(d1.toString());
                    d1.setDate(d1.getDate()+1);
                    }
                    while (d1 <= d2);

                        alert("Dates!!!" +  aDates);
                    //alert(aDates.join("\n")); 


    }

    });

Teraz jestem w stanie dostać się między 2 datami w formacie domyślnym, muszę zmienić domyślny format do MM / DD / YYYY.

Możesz mi zaproponować ....

0
user3918280 12 sierpień 2014, 14:13

2 odpowiedzi

Najlepsza odpowiedź

Jak zgodnie z komentarzem, chcesz następne 7 dni od wybranej daty w formacie DD / MM / YY.The JavaScript jest

$("#startdate").datepicker({
    dateFormat: 'dd/mm/yy'
});


$("#getDate").click(function () {
    var start = $("#startdate").datepicker("getDate"),
    currentDate = new Date(start),
    between = [];
    for(var i=0;i<7;i++) {
        currentDate.setTime(currentDate.getTime() + 24*60*60*1000);
                                        //miliseconds in 1 day
        between.push(new Date(currentDate));

    }
     for (var i = 0; i < between.length; i++) {
        var date = $.datepicker.formatDate('dd/mm/yy', new Date(between[i]));
        between[i] = date;
    }

    console.log(between)//between is the required result
})

ZOBACZ DEMO TUTAJ

0
Roshan 13 sierpień 2014, 05:14

Możesz użyć poniżej funkcji, aby zmienić format daty:

function changeDateFormat(date) {
      var year = date.getFullYear();
      var month = (1 + date.getMonth()).toString();
      month = month.length > 1 ? month : '0' + month;
      var day = date.getDate().toString();
      day = day.length > 1 ? day : '0' + day;
      return month + '/' + day + '/' + year ;
}

EDYTUJ

Stworzyłem skrzypce.

Kiedy tworzysz tablicę aDates w tym czasie, musisz zmienić format daty i trzeba nacisnąć go do tablicy. Patrz poniżej kodu:

$( document ).ready(function() {

          $( "#day" ).click(function() {

                 startJsonSession();
                  return false;
            });
            function changeDateFormat(date) {
              var year = date.getFullYear();
              var month = (1 + date.getMonth()).toString();
              month = month.length > 1 ? month : '0' + month;
              var day = date.getDate().toString();
              day = day.length > 1 ? day : '0' + day;
              return month + '/' + day + '/' + year ;
        }
            function  startJsonSession(){

            var inputdate=$('#inputdate').val();
            //alert("Input Date!!!" + inputdate );

                var d = new Date(inputdate);

                var nowMS = d.getTime(); // get # milliseconds for today
                //alert(nowMS);
                var week = 1000*60*60*24*7; // milliseconds in one week
                //alert(week);
                var oneWeekFromNow = new Date(nowMS + week);
                //alert("oneWeekFromNow!!!" + oneWeekFromNow);
                var fromdate = d.getDate();

                var month = d.getMonth() + 1;
                var year = d.getFullYear();
                if (fromdate < 10) {
                    fromdate = "0" + fromdate;
                }
                if (month < 10) {
                    month = "0" + month;
                }
                //var date = fromdate + "/" + month + "/" + year;
                var date = year + "/" + month + "/" + fromdate;
                alert("InputDate!!!!" + date);

                //var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
                var weekdate=year + "/" + month + "/" + oneWeekFromNow.getDate();
                alert("weekdate!!!" + weekdate);


                var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));

                var tomorrowdate = tomorrow.getDate();

                var month1 = tomorrow.getMonth() + 1;
                var year1 = tomorrow.getFullYear();
                if (tomorrowdate < 10) {
                    tomorrowdate = "0" + tomorrowdate;
                }
                if (month1 < 10) {
                    month1 = "0" + month1;
                }

                //var nextday = tomorrowdate + "/" + month1 + "/" + year1;
                var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
                alert("tomorrow!!!!" + nextday);


                    var d1 = new Date(date);
                    alert("D1!!!!!" + d1);
                    var d2 = new Date(weekdate);
                    var aDates = [];

                    do
                    {
                    var formattedDate = changeDateFormat(d1);
                    aDates.push(formattedDate);
                    d1.setDate(d1.getDate()+1);
                    }
                    while (d1 <= d2);

                        alert("Dates!!!" + aDates);
                    //alert(aDates.join("\n")); 


    }

    });
0
Rupali 13 sierpień 2014, 06:55