如何计算两个日期选择器之间的差异

时间:2014-12-09 21:38:26

标签: javascript jquery datepicker

我试图计算JavaScript中两个日期选择器的日期不同。我试过跟随其他帖子,但每次我计算日期差价*价格,但我没有得到结果。有人可以指导我正确的方向,以实现我找到两个日期选择器之间的日期差异的目标。

    //input/saving data for the next form
$(".next").click(function(){
start = $('#checkin').val();
ends = $('#checkout').val();
diff = (ends - start);
days = diff/1000/60/60/24;

//roomPrice = price inserted into selection type(Single = 65, Double = 100, Suite = 120)
roomPrice = Rprices[selectType.value];

//preparing form5
fields = [$('#FirstName').val() + ' ' + $('#LastName').val(),
$('#phone').val(),
$('#email').val(),
$('#StreetAddress').val(),
$('#City').val(),
$('#state').val(),
$('#zip').val(),
$('#checkin').val(),
$('#checkout').val(),
$('#RoomType').val(),
$('#NumOfPeople').val(),
document.getElementById("total").value = days * roomPrice];

2 个答案:

答案 0 :(得分:0)

我不确定你在问什么,但是:

start = $('#checkin').val(); // assuming this is a date
ends = $('#checkout').val(); // assuming this is a date
var start_date = new Date(start); // assuming the format is correct
var end_date = new Date(ends); // assuming the format is correct
var diff_ms = end_date.getTime() - start_date.getTime(); // assuming both dates exist

会以毫秒为单位获得时差。

答案 1 :(得分:0)

getDateDiff(time1, time2) {
      var t2 = new Date(time1);
      var t1 = new Date(time2);
      var diffMS = t1 - t2;
      var ret = "";
      ret = ret + parseInt(diffMS / 1000 / 60 / 60).toString() + " hours ";
      diffMS = diffMS - parseInt(diffMS / 1000 / 60 / 60) * 60 * 60 * 1000;
      ret = ret + parseInt(diffMS / 1000 / 60).toString() + " min ";
      diffMS = diffMS - parseInt(diffMS / 1000 / 60) * 60 * 1000;
      ret = ret + parseInt(diffMS / 1000).toString() + " sec";
      return ret ;
    }