0

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.

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.

element.all(by.css('.ui-datatable-data > tr > td:nth-of-type(' + td + ')')).then(function (elements) {
        for (i = 0; i < 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 && text1 != "") {                                
                                if (order == 'ascending') {
                                    if (text1 < text2 || text1 == text2) {
                                        expect(true).toBeTruthy();
                                    }
                                    else {
                                        expect(text1 + ' was not less than ' + text2).toBe(false);
                                    }
                                }
                                else if (order == 'descending') {
                                    if (text1 > text2 || text1 == text2) {
                                        expect(true).toBeTruthy();
                                    }
                                    else {
                                        expect('text 1:' + text1 + '. Was not greater than:' + text2 + '.').toBe(false);
                                    }
                                }
                            }
                        });
                    }
                });
            })(i);
        }
    });

I searched a little and found out that this method via javascript compares a string character by character. So in the example as follows:

Sep 6, 2016, 5:17:16 PM
Sep 7, 2016, 12:42:44 PM

The first date is less than the second date and my test would pass with the above method.

However in this example:
Sep 6, 2016, 5:17:16 PM
Sep 7, 2016, 12:42:44 AM

The first date is still less than the second date but once it gets to 'is P < A' it fails.

Similarly:
Sep 6, 2016, 5:17:16 PM
Sep 7, 2016, 2:42:44 PM

The first date is yet again less than the second but gets to 'is 5 < 2' and fails.

Is there a smarter/easier way to accomplish this? Or am I just really missing something here.

d.rodriguez
  • 319
  • 6
  • 17
  • 2
    Just curious, why cant you use new Date(): and compare them as dates? http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Mike Sep 19 '16 at 18:31
  • I passed text1 and text2 into new Date() and compared the now date objects instead of the string values. Seems to be working. – d.rodriguez Sep 19 '16 at 18:44
  • @d.rodriguez—Do not do that. Parsing of non–standard strings is entirely implementation dependent. Use a parser, either write one for that format yourself (3 or 4 lines of code) or use a library. See [*Why does Date.parse give incorrect results?*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). – RobG Sep 20 '16 at 00:18

1 Answers1

4

The date object will do exactly what you expect: Date Object

A small example using your sample data:

var date = new Date("Sep 6, 2016, 5:17:16 PM ");

var date2 = new Date("Sep 7, 2016, 12:42:44 AM");

console.log(date < date2); // true
VladNeacsu
  • 1,268
  • 16
  • 33
  • can I pass like a variable into new date? I don't have hard coded strings of the dates. I extract the table element's text value and compare it with the next one. So I would assume I could just keep looping and creating new date objects with the X date string? – d.rodriguez Sep 19 '16 at 18:37
  • According to the documentation : Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. – Christopher Sep 19 '16 at 18:39
  • Hmm, I think you can safely use it. I haven't found any specific case where it wouldn't work on modern browsers – VladNeacsu Sep 19 '16 at 19:18
  • 1
    Please do not recommend parsing of strings with the Date constructor (or Date.parse, they are equivalent for parsing), see [*Why does Date.parse give incorrect results?*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Strings should always be parsed manually with a small function or library. – RobG Sep 20 '16 at 00:19
  • I would compare date.getTime() – phil Sep 07 '18 at 05:42