Search code examples
javascriptc#jquerybootstrap-4gettime

Why does JavaScript runtime error: Object doesn't support property or method 'getTime' happen?(getTime is not a function)


The following code is MVC C# cshtml document.

@{
    Layout = null;
    ViewBag.Title = "Test";
}
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script src="~/Scripts/bootstrap-datepicker.js"></script>
<script>
    $(document).ready(init);
    function init() {

        //loadData();
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!
        var yyyy = today.getFullYear();
        if (dd < 10) {
            dd = '0' + dd;
        }
        if (mm < 10) {
            mm = '0' + mm;
        }
        var mdate1 = yyyy + '/' + mm + '/01';
        var mdate2 = yyyy + '/' + mm + '/' + dd;

        $('#TSDATE').datepicker({
            startDate: -Infinity,
            endDate: "today",
            format: "yyyy/mm/dd",
            autoclose: true,
            todayHighlight: true,
            language: 'zh-TW'
        }).datepicker("setDate", mdate1);
        $("#TSDATE").bind("change", function () {
            var TSDATE = $("#TSDATE").val().replace(/\//g, '');
            var TEDATE = $("#TEDATE").val().replace(/\//g, '');
            if (TEDATE == '') {
                $('#TEDATE').val($('#TSDATE').val());
            } else {
                if (TSDATE >= TEDATE) {
                    $('#TEDATE').val($('#TSDATE').val());
                }
            }
        });
        $('#TEDATE').datepicker({
            startDate: -Infinity,
            endDate: "today",
            format: "yyyy/mm/dd",
            autoclose: true,
            //startDate: "today",
            //clearBtn: true,
            //calendarWeeks: true,
            todayHighlight: true,
            language: 'zh-TW'
        }).datepicker("setDate", mdate2);
        $("#TEDATE").bind("change", function () {
            var TSDATE = $("#TSDATE").val().replace(/\//g, '');
            var TEDATE = $("#TEDATE").val().replace(/\//g, '');
            if (TSDATE == '') {
                $('#TSDATE').val($('#TEDATE').val());
            } else {
                if (TEDATE <= TSDATE) {
                    $('#TSDATE').val($('#TEDATE').val());
                }
            }
        });
    }
</script>
<h2>Test</h2>
<div class="input-group input-daterange">
    <input type="text" class="form-control" id="TSDATE">
    <div class="input-group-addon">~</div>
    <input type="text" class="form-control" id="TEDATE">
</div>
<script src="~/Scripts/bootstrap.min.js"></script>

As I run the code,the datepicker of first textbox(#TSDATE) is showing up but fail to write "yyyy + '/' + mm + '/01'".And the second textbox (#TEDATE) totally fail,not showing datepicker.The iexplore comes up the error and shows message "Object doesn't support property or method 'getTime'".The position is in the bootstrap-datepicker.js:

setDate: function(d) {
            this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
        },

So the code in cshtml may fail in here:

  }).datepicker("setDate", mdate1);

The Chrome also pop up "'getTime is not a function". I have done some research about the same question online,the key of the problem may be the .js I used or javascript I wrote.But I can't find the solution the the error. How Could I fix it?


Solution

  • In your code,

    }).datepicker("setDate", mdate1);
    

    mdate1 is not a date it is string.

    getTime() is function on a date object not a string. that is the reason you are getting a error "getTime is not a function.