如何将日期从下拉列表转换为javascript日期格式并增加1天?

时间:2015-10-22 12:21:28

标签: javascript jquery

我有dd / mm / yyyy格式的不同日期的下拉列表,我想将所选日期转换为javascript日期格式,并使用javascript增加1。谁能帮帮我吗。提前谢谢。

这是我的代码:

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);


                }
            });
        }
    });

1 个答案:

答案 0 :(得分:3)

像这样:

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);
    }

示例:

&#13;
&#13;
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);
    }
  });
});
&#13;
<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>
&#13;
&#13;
&#13;