I try to find a solution to my issue but it’s a little bit difficult for me. So I need help
On an input field, I have a date with this format => “mar. 13 décembre 2021”.
This date is displayed like “13/12/2021” on another web page and my goal is to compare the date on the input field with the date displayed on the another web page.
At the beginning, I have done this :
${date}= Get Value //input[@name="Date de fin des offres"]
${datesub}= Get Substring ${date} 5
${dateconvert}= Convert Date ${datesub} date_format=%d %m %Y result_format%Y/%m/%d
Besides, it doesn’t work because the month name isn’t a timestamp format.
So, I think to replace the month name with his corresponding number (ex: january = 01, february = 02 etc…) and thus I could have the good format and compare it with the display on the webpage.
According to https://strftime.org/%B matches month as locale’s full name so something like %d %B %Y could work. The problem is how to make sure you use the correct locale. It would be convenient if DateTime would support configuring that, but I’m not sure how easy that would be to implement.
If this gets more complicated, it probably would be best to move the logic to a custom library implemented in Python.
Thanks you for your answer.
Indeed, when I wrote : ${dateconvert}= Convert Date 28 Décembre 2021 date_format=%d %B %Y result_format=%d/%m/%Y (french date format) => it fails
But when I wrote : ${dateconvert}= Convert Date 28 December 2021 date_format=%d %B %Y result_format=%d/%m/%Y (english date format) => it works.
So your solution is good but I haven’t the good date format in my case.
I should think to an another solution
It ought to be possible to dynamically change the locale Python uses but it’s generally not recommended except when the whole program starts. In other words, not a good solution if you need to support multiple locales.
I think Python standard library doesn’t have a good solution for this problem, but there are external modules like Babel that can be used: http://babel.pocoo.org/en/latest/dates.html
Problems with setting locale during execution are apparently mostly related to using threads. It could be fine if threads aren’t used but I’d still look for other alternatives. Babel looks like a good solution.
I have tried your solution but I don’t understand how I can use the split string as a key.
Here my code :
${date}= Get Value //input[@name="Date de fin des offres"]
${datesub}= Get Substring ${date} 5
@{date_split}= Split String ${datesub}
When I wrote Log ${date_split}[1], the return value is “décembre”.
But I tried something like ${month}= Get Dictionary Values ${month_fr}[${date_split}[1]] but I have an error saying that the expected argument must be a dictionary and he received a string.
I have some difficulties to play with 2 lists.
${date}= Get Value //input[@name="Date de fin des offres"]
${datesub}= Get Substring ${date} 5
@{date_split}= Split String ${datesub}
${month_number}= Get From Dictionary ${month_fr} ${date_split}[1]
${dateconvert}= Convert Date 28 ${month_number} 2021 date_format=%d %m %Y result_format=%d/%m/%Y
Sorry it took me a while to get back to you, been snowed under at work.
your solution is ok, another option would be to do something like this:
${date}= Get Value //input[@name="Date de fin des offres"]
${datesub}= Get Substring ${date} 5
@{date_split}= Split String ${datesub}
${dateconvert}= Convert Date 28 ${month_fr}[${date_split}[1]] 2021 date_format=%d %m %Y result_format=%d/%m/%Y
or even slightly easier:
${date}= Get Value //input[@name="Date de fin des offres"]
${datesub}= Get Substring ${date} 5
@{date_split}= Split String ${datesub}
${dateconvert}= Set Variable 28/${month_fr}[${date_split}[1]]/2021
Or if you want to retain the original date:
${dateconvert}= Set Variable ${date_split}[0]/${month_fr}[${date_split}[1]]/${date_split}[2]