Reputation: 89
I am getting a time stamp by reading a field in my page which has the month name in it like "Feb 28, 2015 12:59". I want to verify which month name it is and assign it the month number. I want to get the date such as "5 28, 2015 12:59". I have divided the time stamp and I am trying to convert the month name to number as below:
|| ${monthNmToNum} | Set Variable If |'${monthAbr}' == 'Feb' | 2
|| ${monthNmToNum} | Set Variable If |'${monthAbr}' == 'Mar' | 3
|| ${monthNmToNum} | Set Variable If |'${monthAbr}' == 'Apr' | 4
but the issue is that if the month name is "Feb" in the date stamp then ${monthNmToNum} becomes "none" at the end. What I actually want to achive is to convert this time stamp to epoch. I am using RIDE for robot framework. Let me know if I am missing some info above.
Upvotes: 0
Views: 3090
Reputation: 1849
Try some of the percent-format code directives listed in https://docs.python.org/3/library/datetime.html for instance: %b Month as locale’s abbreviated name.
Thus: ${datetime_standard} = Convert Date 1977 May 4 date_format=%Y %b %d
yields: 1977-05-04 00:00:00.000
Upvotes: 0
Reputation: 1188
We have DateTime Library, which we can use to convert the date and Python supports different date formats
See the code below:
${MonthNumber}= Convert Date ${Date} result_format=%m
It will give you month as number, it solves your problem.
Upvotes: 0
Reputation: 385900
A simple solution is to create a list that contains the month abbreviations, and then ask python to look up the abbreviation in the list. This will work as long as you know for certain what the abbreviations will be, and they will always be in the list:
*** Variables ***
| @{MONTHS} | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
*** Testcase ***
| Convert month abbreviation to number
| | ${monthAbr}= | Set variable | Feb
| | ${month}= | evaluate | ${MONTHS}.index("Feb") + 1
| | Should be equal as numbers | ${month} | 2
However, if your ultimate goal is to get the epoc, robot has a built-in keyword to do that in the DateTime library. That was answered in this other question you asked earlier: Convert string to seconds - Robot framework.
Upvotes: 1