我有一个在chrome中工作的倒数计时器,但是当我在野生动物园中查看时,所有数字都显示NAN,而且如果我设置了过去的日期,也不会在else语句中触发我的模型。我在网上寻找解决方案,但没有找到解决方案。

  $(document).ready(function() {

        $('#popupTimer').delay(1000).fadeIn(600);

        // Set the date we're counting down to  (*** Set to Apr 9th after testing ***)
        var countDownDate = new Date("Apr 3, 2017 24:00:00").getTime();

        // Update the count down every 1 second
        var x = setInterval(function() {

            // Get todays date and time
            var now = new Date().getTime();

            // Find the distance between now an the count down date
            var distance = countDownDate - now;

            // Time calculations for days, hours, minutes and seconds
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);

            // Display the result in the element with id="display"
            document.getElementById("display").innerHTML = days + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds ";

            // If the count down is finished,
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("display").innerHTML = "EXPIRED";
                $('#myModal').modal('show');
                $(".myDIV").hide();
                $('#chooseProductThree').show();
                $(".panel-heading").addClass("active-panel");
            }
        }, 1000);
    });

最佳答案

首先写下面的功能

function parseDateString (dateString) {
            var matchers = [];
            matchers.push(/^[0-9]*$/.source);
            matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
            matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
            matchers = new RegExp(matchers.join("|"));
            if (dateString instanceof Date) {
                return dateString;
            }
            if (String(dateString).match(matchers)) {
                if (String(dateString).match(/^[0-9]*$/)) {
                    dateString = Number(dateString);
                }
                if (String(dateString).match(/\-/)) {
                    dateString = String(dateString).replace(/\-/g, "/");
                }
                return new Date(dateString);
            } else {
                throw new Error("Couldn't cast `" + dateString + "` to a date object.");
            }
        }



  ↓↓↓↓↓然后按如下所示调用此功能↓↓↓↓↓


var EndTime = "2019-05-10 00:00:00";
countDownDate=Date.parse(_self.parseDateString(EndTime));

09-10 18:22