Rahul
Rahul

Reputation: 759

How to compare a String value to a date in YYYY-MM-DD format in protractor node js

From the application, am capturing the date field in string format with the getText() function.I want to check if the captured date is less than the current date or not. Can some one pls help.

var actDate = locator.getText();

This gives me the output as "2021-04-23 01:11:10 GMT" .I will split the output to get only the date as "2021-04-13" .

var date=actDate.split(' ')[0];

After this, I wanted to compare with the current date.Am able to get the current date as :

this.todayDate= function(){
    return moment(Date.now()).format('YYYY-MM-DD');
   }

This gives me the output as 2021-04-23

Can someone help me how to compare these two values. I want to check the date returned from application is lesser the current date.

Upvotes: 0

Views: 259

Answers (2)

deko_39
deko_39

Reputation: 68

Compare the string directly. For e.g. "2021-04-13" < "2021-04-23"

This will return true.

Upvotes: 2

Freddy Lebogo
Freddy Lebogo

Reputation: 66

Try this

   var date = actDate.split(' ')[0]; // date = "2021-04-13"
     var today = new Date().toISOString().slice(0, 10) // get current date
            
     if(Date.parse(date) < new Date(today)){
        //captured date is less than the current date
     }

Upvotes: 1

Related Questions