0

我的日期格式是 dd/mm/yyyy 并计算这种格式

这里有一个脚本

<script type="text/javascript">
  function GetDays() {

    var dropdt = new Date(document.getElementById("sdate").value);
    var pickdt = new Date(document.getElementById("edate").value);
    var difference = edate - sdate;
    return Math.round(difference / 1000 * 3600 * 24);
  }

  function cal() {

    document.getElementById("numdays2").value = GetDays();

  }
</script>
4

2 回答 2

1

您需要从日期获取时间,然后进行计算

var date1 = new Date("06/30/2019"); 
var date2 = new Date("07/30/2019"); 

// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime(); 

// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24); 

//To display the final no. of days (result)
console.log("Total number of days between dates  <br>"
               + date1 + "<br> and <br>" 
               + date2 + " is: <br> " 
               + Difference_In_Days); 
于 2019-11-30T19:52:45.497 回答
0

首先,标识符错误,其次使用 getTime 函数获取毫秒数,在第三位添加括号到评估表达式以获取天数差异

var difference = dropdt.getTime() - pickdt.getTime();
return Math.round(difference / (1000 * 3600 * 24));
于 2019-11-30T19:34:54.707 回答