1

I want to get tomorrow date, but this work not correctly. I dont understand, which problem is here, I split date, for getting true date format, check if checkbox checked and add to getdate 1 day, and shape date as i want


function doLateCheckOut(checkbox){
    var end_date = $('[name="end_date"]').val();
    var dateArr = end_date.split(".");
    var date = new Date(dateArr[2]+'-'+dateArr[1]+'-'+dateArr[0]);

    if(checkbox.checked) {
        date.setDate(date.getDate() + 1);

    }
    else{

        date.setDate(date.getDate() - 1);
    }

    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();
    day = day + '';

    if (day.length == 1) {
        day = "0" + day;
    }

    if (monthIndex < 10) {
        monthIndex = "0" + (monthIndex+1);
    }

    var latedate = day + "." + monthIndex + "." + year;

    $('[name="end_date"]').val(latedate);
    console.log(latedate);

}
  • what error or wrong date you are getting? you must know that the monthIndex in date starts from 0. And I will suggest using [moment](https://momentjs.com/docs/) for the requirement. – AZ_ Mar 26 '19 at 07:34
  • Can you please post the error so we can analyze what could be incorrect – Apoorv Mar 26 '19 at 07:38
  • Yes, sure I use month index not correctly, but it also not work correctly, get 2 day after... I havent error, i get wrong date. – Gunay Rzayeva Mar 26 '19 at 09:59
  • I dont know why, but i get 1 day before from new Date() – Gunay Rzayeva Mar 26 '19 at 10:18

0 Answers0