繁体   English   中英

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

[英]How to calculate the different between two datepickers

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

    //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];

我不确定您要问什么,但:

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

将为您提供时差,以毫秒为单位。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM