Home > Mobile >  Protractor: How to sort a list by date/time?
Protractor: How to sort a list by date/time?

Time:11-08

How do I sort a list by date/time? in my application, the date/time format is "Mon, Nov 1 | 8:30 PM" when I try to convert the string into date getting an invalid date as o/p. Help me to sort a list by date/time.

Related to this question

CodePudding user response:

the date/time "Mon, Nov 1 | 8:30 PM" don't have 'year'

enter image description here

CodePudding user response:

I have hardcoded and trimmed that '|' character from the date/time format. Able to sort refer the below snippet. And I stuck in how could I remove that '|' while getting from xpath.

let unsortedDate = [ "Tue, Nov 2 6:25 PM",

              "Wed, Oct 28 12:30 AM",

              "Mon, Nov 8 3:05 AM",

              "Mon, Nov 1 8:30 PM"];

var sort = [];
unsortedDate.forEach(date => {
    sort.push([ date, new Date(date).getTime() ]);
})
console.log(sort);

sort.sort( (a, b)  => a[1] - b[1] );

let sorted_array = sort.map(arr => arr[0])

console.log(sorted_array)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related