从IF条件Jquery获取日期时返回NaN

时间:2018-02-19 13:21:12

标签: javascript jquery

我正在尝试根据所选的单选按钮更改日期。

这是我的代码,因为它将日期作为NaN返回而无效。

FIDDLE

var minDate;
var maxDate;
var startDate;
var endDate;

$("input[name=package]:radio").click(function() {

  if ($('input[name=package]:checked').val() == "six") {
    alert(minDate);
    minDate = new Date(2018, 7 - 1, 1);
    maxDate = new Date(2018, 6 - 1, 11);
    startDate = new Date(2018, 6 - 1, 3);
    endDate = new Date(2018, 6 - 1, 7);

  } else if ($('input[name=package]:checked').val() == "four") {
    alert(minDate);
    minDate = new Date(2018, 6 - 1, 1);
    maxDate = new Date(2018, 6 - 1, 11);
    startDate = new Date(2018, 6 - 1, 4);
    endDate = new Date(2018, 6 - 1, 10);


  }
});



accommodation_nights = 6;
pay_values = new Array();
pay_values_sum = 0;
booked_nights = 0;
optional_price = 0;
share_transfer_price = 0;
register_price = 0;
room_type = "";
final_price = 0;
previous_transfer = "";
accomp = false;
accomp_price = "";
monastery = 0;
event = 0;


var minFlightDate = new Date(2018, 6 - 1, 1);
var maxFlightDate = new Date(2018, 6 - 1, 30);
var earlyDate = new Date(2018, 6 - 1, 15);
var lateDate = new Date(2018, 9 - 1, 1);
var currentDate = new Date();





function string_to_date(dateString) {
  var string_to_date = dateString.split("/");
  var outDate = new Date(string_to_date[2], string_to_date[1] - 1, string_to_date[0]);
  return (outDate);
}

function date_to_string(date) {
  var d = new Date(date),
    month = '' + (d.getMonth() + 1),
    day = '' + d.getDate(),
    year = d.getFullYear();

  if (month.length < 2)
    month = '0' + month;
  if (day.length < 2)
    day = '0' + day;

  return [day, month, year].join('/');
}

function process_nights(firstDate, lastDate) {
  if ($('.arrivalDate').val() && $('.departureDate').val()) {
    var arrivalDate = string_to_date($('.arrivalDate').val());
    var departureDate = string_to_date($('.departureDate').val());

    if (firstDate !== undefined && lastDate != undefined) {
      if (arrivalDate.getTime() > firstDate.getTime()) {
        $('.arrivalDate').val(date_to_string(startDate)).addClass("has-value");
        alert("Date of arrival should not be later than " + date_to_string(firstDate));
      }
      if (departureDate.getTime() < lastDate.getTime()) {
        $('.departureDate').val(date_to_string(endDate)).addClass("has-value");
        alert("Date of departure should not be earlier than " + date_to_string(lastDate));
      }
    }
    if (arrivalDate.getTime() >= departureDate.getTime()) {
      if (lastDate != undefined) {
        $('.departureDate').val(date_to_string(lastDate));
      } else {
        $('.departureDate').val("");
      }
      alert('Date of departure must be later than date of arrival.');
      $('input[name="nightsCount"]').val("");
      $('input[name="nightsCount"]').removeClass("has-value");
      return;
    }
    var timeDiff = Math.abs(departureDate.getTime() - arrivalDate.getTime());
    var nightsCount = Math.ceil(timeDiff / (1000 * 3600 * 24));
    $('input[name="nightsCount"]').val(nightsCount);
    $('input[name="nightsCount"]').addClass("has-value");
    return nightsCount;
  }
}


// personalTransfer


$(function() {
  $("body").on("change", 'input[type=text]', function(e) {
    $(this).val($(this).val().trim())
  });
  //personalTransferAirport
  $("body").on("click", "input[name='personalTransferAirport']", function(e) {
    if ($(this).val() != "") {
      $(".personal_transfers").show();
      var airport = $(this).val();
      var airport_texts = $(".airport_text");
      for (var i = 0; i < airport_texts.length; i++) {
        $(airport_texts[i]).text($(airport_texts[i]).text().replace("%airport%", airport));
        $(airport_texts[i]).text($(airport_texts[i]).text().replace("Bourgas", airport));
        $(airport_texts[i]).text($(airport_texts[i]).text().replace("Varna", airport));
        $("input[name='personalTransfer']").attr("required", "required");
      }
    } else {
      $("input[name='personalTransfer']").prop("checked", "");
      $("input[name='personalTransfer']").removeAttr("required");
      $(".personal_transfers").hide();
      $(".share_row").show();
    }
  });
  $("body").on("click", "input[name='personalTransfer']", function(e) {
    personal_transfers();
  });

  $("body").on("click", ".personal_multiplier", function(e) {
    personal_transfers();
  });




  $('.arrivalDate').val(date_to_string(startDate)).addClass("has-value");
  $('.departureDate').val(date_to_string(endDate)).addClass("has-value");
  booked_nights = process_nights();

  $('.datepicker').datepicker({
    firstDay: 1,
    dateFormat: 'dd/mm/yy',
    minDate: minDate,
    maxDate: maxDate
  });

  $('.datepicker_visa').datepicker({
    firstDay: 1,
    dateFormat: 'dd/mm/yy',
    changeMonth: true,
    changeYear: true,
    yearRange: '2000:2030'
  });

  $('.datepickerFlight').datepicker({
    firstDay: 1,
    dateFormat: 'dd/mm/yy',
    minDate: minFlightDate,
    maxDate: maxFlightDate
  });

  $('.datepicker').on('change', function() {
    process_nights(startDate, endDate);
    booked_nights = process_nights();
    show_payment_values();
  });

  $('.datepicker_visa').on('change', function() {
    process_visa_dates();
  });



})

function add_pay_values() {
  $('#payment_value').find('option').remove();
  for (i = 0; i < pay_values.length; i++) {
    $("#payment_value").append('<option value="' + pay_values[i] + '">' + pay_values[i] + '€</option>');
  }
}

function show_payment_values() {
  $(".sum").show();
  pay_values = new Array();
  pay_values_sum = 0;
  var other_taxes = 0;
  var event = 0;
  var extra_nights_price = 0;


  if (register_price != 0) {
    pay_values_sum += register_price;
    pay_values.push(pay_values_sum);
  }
  if (accommodation_nights != booked_nights) {
    if (booked_nights - accommodation_nights > 0) {
      if (room_type == "single") {
        extra_nights_price += (booked_nights - accommodation_nights) * 82;
        $("input[name='accommodationTax']").val(82);
      } else {
        extra_nights_price += (booked_nights - accommodation_nights) * 57;
        $("input[name='accommodationTax']").val(57);
      }
    }
  }

  if (accomp) {
    other_taxes = 0;
    pay_values_sum += accomp_price;
    other_taxes += accomp_price;
    pay_values.push(pay_values_sum);
    if (extra_nights_price != 0) {
      other_taxes += extra_nights_price;
      extra_nights_price = extra_nights_price * 2;
      pay_values_sum += extra_nights_price;
      pay_values.push(pay_values_sum);
    }
  } else if (extra_nights_price != 0) {
    //			other_taxes = 0;
    pay_values_sum += extra_nights_price;
    pay_values.push(pay_values_sum);
    //			other_taxes += extra_nights_price;
  }

  //		if (extra_nights_price != 0) {
  //			pay_values_sum += extra_nights_price;
  //			if (accomp) {
  //				pay_values_sum += extra_nights_price;
  //			}
  //			pay_values.push(pay_values_sum);
  //		}
  //		if (accomp) {
  //			pay_values_sum += accomp_price;
  //			pay_values.push(pay_values_sum);
  ////			extra_nights_price = extra_nights_price * 2;
  //		}
  //accomp




}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><table align="center">
  <tr>

    <td>

      <label><input type="radio" name="package" value="six"> <strong>6 nights’ All-inclusive accommodation</strong></label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>

    <td>

      <label><input type="radio" name="package" value="four"><strong>4 nights’ All-inclusive accommodation</strong></label>
    </td>

  </tr>

</table>
<div class="date">
  <div class="form-group col-4">
    <input type="text" class="datepicker arrivalDate" name="arrivalDate" data-holder="date" />
    <label for="input" class="control-label">Date of arrival</label><i class="bar"></i>
  </div>
  <div class="form-group col-4">
    <input type="text" class="datepicker departureDate" name="departureDate" data-holder="date" />
    <label for="input" class="control-label">Date of departure</label><i class="bar"></i>
  </div>
  <div class="form-group col-4">
    <input type="number" name="nightsCount" class="has-value">
    <label for="input" class="control-label">Number of nights</label><i class="bar"></i>
  </div>
</div>

0 个答案:

没有答案