如何找到两个日期之间的差异?

时间:2019-11-19 07:44:46

标签: javascript date datetime

嗨,我有两个约会 firstDate =“ 06/11/2019” lastDate =“ 15/11/2019 00:00:00”

var date1 = new Date(firstDate);
var date2 = new Date(lastDate);
var diffTime = Math.abs(date2 - date1);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

我得到diffDays = NaN。我怎样才能做到这一点。谢谢。

1 个答案:

答案 0 :(得分:0)

您的 lastDate 是无效的日期。 您可以使用

进行检查
 <!--/* Thymeleaf templating engine, Each token will be replaced by their respective titles in the 
 resulting page. */-->
<    html xmlns:th="http://www.thymeleaf.org" layout:decorate="~{layout}">

<head>
<title>Share Contact Info</title>
<meta charset="UTF-8">
<title>Share Contact Info</title>
   <!-- <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" 
rel="stylesheet">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>
    -->
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.css" 
    rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.js"></script>
</head>

<body>
    <div layout:fragment="breadcrumb">
    <h1>Share Contact Info</h1>
    <ol class="breadcrumb">
        <li><a href="#" th:href="@{/}"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Share Contact Info</li>
    </ol>
   </div>
<div layout:fragment="content">
    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">&nbsp;</h3>
        </div>
        <!-- /.box-header -->
        <!-- form start -->
        <div id="summernote">

        </div>
        <form role="form" th:object="${shareContactInfo}" method="post">


            <script>
                $(document).ready(function () {

                    $('#summernote').summernote();


                });
            </script>

            <div class="form-group">
                <button type="submit" class="box-title btn btn-primary" title="Save 
 Data">Save</button>
            </div>
        </form>

    </div>
</div>

这将返回NaN

检查“警告”部分 https://www.w3schools.com/js/js_date_formats.asp