Replace month name by his number

Hello,

I try to find a solution to my issue but it’s a little bit difficult for me. So I need help :slight_smile:

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.

Have you an idea how can I do this ?

John

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.

Hello,

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 :smiley:

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

I just watching how to set the locale Python lol, but if you say that is not recommanded… I don’t go further…

Thanks for your help, I’m going to read your proposal.
Otherwise, I will try to code it (difficult for someone who are not developer !)

Thanks again !

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.

Well this is another way of looking at the problem, maybe not as elegant, but may work for you.

*** Variables ***
&{month_fr}=    janvier=1    février=2    mars=3    avril=4    mai=5    juin=6    juillet=7    aout=8    septembre=9    octobre=10    novembre=11    décembre=12

Then with Split String break the date apart, use the month component of the split sstring as the key for &{month_fr} to return the number.

then if you need to deal with another site in another language same approach and no stitching locals.

Hope that helps, if nothing else allows you to look at the problem differently,

Dave.

Hello,

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.

Thanks for help !

Hello,

For information, I have found the solution :slight_smile:

${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

It’s not optimized but… it works !

Thanks for your help :slight_smile:

Hi John,

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]

Dave.

Thank you Dave,

Don’t worry about the response time. It is always rewarding to read your comments.

1 Like