Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

jQuery DateTimePicker compare selected date with current date

When a user selects a date from my DateTimePicker it has the date like 01/07/2021, when comparing to the current day to ensure it's not a date in the past, my check formats the date like 1/7/2021 and then my code throws the error.

In this scenario it shouldn't throw an error as the dates are exactly the same, just for the additional 0's. What am I doing wrong?

HTML -

                                <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                                    <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" />
                                    <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                                        <div id="dateTime" class="input-group-text"><i class="fa fa-calendar"></i></div>
                                    </div>
                                </div>

JQuery -

    //create datetimepicker
    $(function () {
        $('#datetimepicker1').datetimepicker({
            sideBySide: true,
            format: 'L',
            // restrict past dates being selected
            minDate: new Date()
        });
    });


        var dateTime = $("#datetimepicker1").data().date;

        // get current Date to check against user input
        var currDate = new Date();
        var dd = currDate.getDate();
        var mm =  currDate.getMonth()+1;
        var yyyy = currDate.getFullYear();
        currDate = mm + '/' + dd + '/' + yyyy;

        // check if user date is in the past. If in past then throw error & fail.
        if (dateTime < currDate)
        {
            // throw error
            alert('Error - Invalid Date. All dates must be either in the present or future.')
            $("#datetimepicker1").datetimepicker("clear");
            return false;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...