JS date format and date comparison

后端 未结 2 1511
遇见更好的自我
遇见更好的自我 2021-01-22 04:02

I Have a date in this format

 16-NOV-12 

From an input box with id = date.

How do I check if this date is in the past of future?

相关标签:
2条回答
  • 2021-01-22 04:39

    Try something like this:-

     var selectedDate = $('16-Nov-12').datepicker('getDate');
     var now = new Date();
     if (selectedDate < now) {
        // selected date is in the past
     }
    
    0 讨论(0)
  • 2021-01-22 04:44
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"
            type="text/javascript"></script>
            <script src="http://www.datejs.com/build/date.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(document).ready(function () {
                    var idstring = "16-NOV-12";
                    var time = Date.compare(Date.parse("16-NOV-2012"), Date.today());
                    var now = "in past";
                    if (time == 0) now = "today";
                    if (time > 0) now = "in future";
                    alert(idstring + " is " + now);
                });
            </script>
        </head>
        <body>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题