'How to test if two dates are different using RobotFrameWork
I wrote a code that calls a webservice twice and recover a date field called : lastLocationDate. In my code I stock the two dates in two different variables date1 and date2 .
Now what I want is to make sure that my two dates are not the same. when I log the value of my dates I have :
Date1= 2019-10-08T06:54:06.118Z
Date2= 2019-10-08T06:54:05.783Z
I tried with substract it didn't work I am trying with not equal and looking for something ilke different.
${DateFinale}= Subtract Time From Date ${Date2} ${Date1} result_format=%y-%m-%d exculse_millies=True date_Format=%Y-%m-%dT%H:%M:%S.%f
I have also tried to add the later Z in the code it dodn't work too
date_Format=%Y-%m-%dT%H:%M:%S.%fZ
I axpected a TimeDate but I got this mesage:
KEYWORD ${DateFinale} = DateTime . Subtract Time From Date ${Date2}, ${Date1}, result_format=%y-%m-%d, exculse_millies=True, date_Format=%Y-%m-%dT%H:%M:%S.%f
Documentation:
Subtracts time from date and returns the resulting date.
Start / End / Elapsed: 20191008 09:08:18.494 / 20191008 09:08:18.494 / 00:00:00.000 09:08:18.494 FAIL Keyword 'DateTime.Subtract Time From Date' got positional argument after named arguments.
Solution 1:[1]
Shorter version than earlier answer:
Compare Dates Are Different
${date1}= Get Current Date
${date2}= Get Current Date
Run Keyword If '${date1}' == '${date2}' Fail Dates are same
Solution 2:[2]
You could try it this way:
${isdateequal}= Run Keyword And Return Status Should Be Equal ${date1} ${date2}
Run Keyword If '${isdateequal}' == 'True' Test Failed
Solution 3:[3]
I found a simple way to solve the problem All I had to do is to compare my dates as Strings
Should Not Be Equal As Strings ${Date1} ${Date2} msg=Aucune localisation n'a été recue
Thank you for your help
Solution 4:[4]
I would use Evaluate Keyword
${date1}= Convert Date 2019-10-08T06:54:06.118Z
${date2}= Convert Date 2019-10-08T06:54:05.783Z
${date_same}= Evaluate '${date1}' == '${date2}'
Log ${date_same}
Should Not Be True ${date_same}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | asprtrmp |
| Solution 2 | |
| Solution 3 | Hopeful |
| Solution 4 | Francois |
