title: Comparing two date values (as string) in javascript
body: <p>I have a table of 'dates created' and this column is sort-able by ascending and descending order which I'm using Protractor/Jasmine in JavaScript to perform my tests.</p>
  
  <p>Currently I was doing the following, which works, but I wasn't dealing with any dates that were created in the AM and then found my errors.</p>
  
  <pre><code>element.all(by.css('.ui-datatable-data &gt; tr &gt; td:nth-of-type(' + td + ')')).then(function (elements) {
          for (i = 0; i &lt; elements.length; i++) {
              (function (index) {
                  elements[index].getText().then(function (text1) {
                      if (index != elements.length - 1) {
                          elements[index + 1].getText().then(function (text2) {
                              if (text2 != undefined &amp;&amp; text1 != "") {                                
                                  if (order == 'ascending') {
                                      if (text1 &lt; text2 || text1 == text2) {
                                          expect(true).toBeTruthy();
                                      }
                                      else {
                                          expect(text1 + ' was not less than ' + text2).toBe(false);
                                      }
                                  }
                                  else if (order == 'descending') {
                                      if (text1 &gt; text2 || text1 == text2) {
                                          expect(true).toBeTruthy();
                                      }
                                      else {
                                          expect('text 1:' + text1 + '. Was not greater than:' + text2 + '.').toBe(false);
                                      }
                                  }
                              }
                          });
                      }
                  });
              })(i);
          }
      });
  </code></pre>
  
  <p>I searched a little and found out that this method via javascript compares a string character by character.  So in the example as follows:</p>
  
  <p>Sep 6, 2016, 5:17:16 PM
  <br>Sep 7, 2016, 12:42:44 PM</p>
  
  <p>The first date is less than the second date and my test would pass with the above method.<br>
  <br>However in this example:<br>
  Sep 6, 2016, 5:17:16 PM
  <br>Sep 7, 2016, 12:42:44 <strong>AM</strong><br>
  <br>The first date is still less than the second date but once it gets to 'is P &lt; A' it fails.<br>
  <br> Similarly:
  <br>Sep 6, 2016, 5:17:16 PM <br>
  Sep 7, 2016, <strong>2</strong>:42:44 PM<br>
  <br>The first date is yet again less than the second but gets to 'is 5 &lt; 2' and fails.</p>
  
  <p>Is there a smarter/easier way to accomplish this? Or am I just really missing something here.</p>