Compare dates using .datescompare()

.datescompare()


Description: Compare dates to each other.


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>datescompare demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
        
        <div class="row">
            <b>Date 1:</b><br />
            <input type="text" name="example_date_1" class="example_date_1" value="" />
        </div>
        
        <div class="row">
            <b>Date 2:</b><br />
            <input type="text" name="example_date_2" class="example_date_2" value="" />
        </div>
        
        <div class="row">
            <b>Result:</b><br />
            <span class="results"></span>
        </div>
        
        <div class="row">
            <input type="button" name="submit" onclick="validate();" value="Compare" />
        </div>
        
    <script>
        
        function validate() {
            var results = bitsyCode.datescompare(bitsyCode(".example_date_1").val(), bitsyCode(".example_date_2").val());
            var object = "";
            for (var key in results) {
                object += "&nbsp;&nbsp;"+key + " = " + results[key] + "<br />";
            }
            
            var response = "Comparing: <b>" + bitsyCode(".example_date_1").val() + "</b> ";
                response+= "to <b>" + bitsyCode(".example_date_2").val() + "</b><br /><br />{<br />" + object + "}";
            bitsyCode(".results").html(response);
        }
        
    </script>
     
    </body>
    </html>

Preview:

Date 1:

Date 2:

Result:

  • Usage

    • How to use:

      Specify two date inputs and compares the first date to the second.

    • USAGE:

      bitsyCode.datescompare("2015-11-09", "2015-11-10")

      OR

      bitsyCode.datescompare("2015/11/09", "2015/11/10")

      There are 2 types of date formats that can be used to compare either '-' or '/'. eg.(2015-11-09 or 2015/11/09)

  • Options

    • What parameters are there:
      There are no options required, however you will need to specify 2 dates as parameters to compare against each other. (View Usage above.)
  • Returns

    • Returns:

      The return type is an object.

    • Return:
      Object { less: true/false, greater: true/false, lessequal: true/false, greaterequal: true/false, equal: true/false, daysdiff: 0 }
    • Object Keys:
      less: true / false  // 'true' if Date 1 is LESS than Date 2 else 'false'
      greater: true / false // 'true' if Date 1 is GREATER than Date 2 else 'false'
      lessequal: true / false // 'true' if Date 1 is LESS than or EQUAL to Date 2 else 'false'
      greaterequal: true / false // 'true' if Date 1 is GREATER than or EQUAL to Date 2 else 'false'
      equal: true / false // 'true' if Date 1 is EQUAL to Date 2 else 'false'
      daysdiff: 0 // (numeric value) days between the two dates
Share