Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how convert date from dropdown to javascript date format and increment by 1 day?

I have a dropdown for different dates in dd/mm/yyyy format and i want to convert the selected date to javascript date format and increment by 1 using javascript. Can anyone please help me. Thanks in advance.

Here is my code:

HTML

<select name="txtweek" id="txtweek">
   <option>--select--</option>
   <option>31-08-2015</option>
   <option>07-09-2015</option>
   <option>14-09-2015</option>
   <option>21-09-2015</option>
   <option>28-09-2015</option>
   <option>05-10-2015</option>
   <option>12-10-2015</option>
   <option>19-10-2015</option>
</select>

Javascript

jQuery("#txtweek").change(function(){
        if(jQuery("select[name='txtuser'] option:selected").text()=='--select--'){          
            alert("Please select a user first to proceed");
            return false;
        } else {
            jQuery.ajax({
                url:ajaxurl,
                type:'post',
                data:{action:'timecardapp_manage_timecard_stepone',user_id: jQuery("#txtuser").val()},
                success: function(response){    
                    var data=JSON.parse(response);  
                    var currentweek = jQuery("#txtweek").val();
                    var finalcurrentweek = new Date(currentweek);
                    var next_day = finalcurrentweek.setDate(finalcurrentweek.getDate()+1);;
                    var trHTML = '';
                    jQuery.each(data.final_data, function (i, item) {
                        if(item.event_title == 'Admin'){
                            //Monday
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>Mon</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';

                            //Tuesday
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>Tue</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';

                            //Wednesday
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>Wed</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';

                            //Thursday
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>Thu</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';

                            //Friday
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>Fri</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';
                        } else{
                            trHTML += '<tr>';
                            trHTML += '<td>'+item.event_title+'</td>';
                            trHTML += '<td>'+item.day_of_week+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.start_date+'</td>';
                            trHTML += '<td>'+item.end_date+'</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>&nbsp;</td>';
                            trHTML += '<td>'+item.expense_allowance_value+'</td>';
                            trHTML += '<td>'+item.hours_allowed+'</td>';
                            trHTML += '</tr>';
                        }

                    });

                    // var trHTML = '';

                    jQuery('.timecard-step2').find('tbody').empty();
                    jQuery('.timecard-step2').find('tbody').append(trHTML);


                }
            });
        }
    });
like image 437
Nitin Johnson Avatar asked Oct 18 '22 23:10

Nitin Johnson


1 Answers

Like this:

function pad(str) {
  return ("0"+str).slice(-2);
}
function getNextDay(str) { 
  var parts = str.split("-");
  var next_day = new Date(parts[2], parts[1] - 1, parseInt(parts[0], 10) + 1, 12, 0, 0, 0);
  return ""+pad(next_day.getDate())+"-"+pad(next_day.getMonth()+1)+"-"+next_day.getFullYear();
}

    var currentweek = jQuery("#txtweek option:selected").text(), // no values available
      next_day = new Date(); // or some other default
    if (currentweek && currentweek.indexOf("--") == -1) { // not the first
      next_day=getNextDay(currentweek);
      console.log(currentweek + '->'+next_day);
    }

Example:

function pad(str) {
  return ("0"+str).slice(-2);
}
function getNextDay(str) { 
  var parts = str.split("-");
  var next_day = new Date(parts[2], parts[1] - 1, parseInt(parts[0], 10) + 1, 12, 0, 0, 0);
  return ""+pad(next_day.getDate())+"-"+pad(next_day.getMonth()+1)+"-"+next_day.getFullYear();
}


 $(function() {
   $("#txtweek").on("change",function() {
    var currentweek = jQuery("#txtweek option:selected").text(), // no values available
      next_day = new Date(); // or some other default
    if (currentweek && currentweek.indexOf("--") == -1) { // not the first
      next_day =getNextDay(currentweek);
      snippet.log(currentweek + '->'+next_day);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<select name="txtweek" id="txtweek">
  <option>--select--</option>
  <option>31-08-2015</option>
  <option>07-09-2015</option>
  <option>14-09-2015</option>
  <option>21-09-2015</option>
  <option>28-09-2015</option>
  <option>05-10-2015</option>
  <option>12-10-2015</option>
  <option>19-10-2015</option>
</select>
like image 97
mplungjan Avatar answered Oct 31 '22 16:10

mplungjan