Time difference between two date fields

Hi Everyone,
I’m new to this robot framework. I have a requirement where i need to get the difference between two date fields of format "mm-dd-yyyy hh:mm:ss AM/PM " Please be informed am/pm must include in the date format.
Approach:
I 'm getting the text value of the above field and able to print values in string format.
Now while convert the string values to Date format by using ‘CONVERT DATE’. i’m getting
value error :
valueError: time data ‘0113-20-22 09:07:48.000000’ does not match format '%Y-%m-%d %H:%M:%S.%f’

Below two dates i’m reading and printing on console as string value.
…Receive Date 01-13-2022 09:22:16 PM
.Regulatory DueDate 02-12-2022 09:22:16 PM

I need difference in days or hours between two days using robot framework.

Since both the date should be in same dateformat to get the time difference. I’m stuck in this scenario to get time difference. Anyone please help with the snippet. If they have. It’s an urgent requirement. Please help.

Regards,
Akshay

Hi @akshay ,

This sounds like the perfect example of when to use Evaluate

Use the Python DateTime module to convert both dates to epoch values, then use Evaluate again to subtract one from the other, so then you have the difference in seconds, from there, divide by 60 for minutes, divide minutes by 60 for hours, hours by 24 for days etc.

Of course you can also create a custom python keyword to do all this in one call.

Hope that helps,

Dave.

Receive Date field value should be present
Sleep 3
Scroll Element Into View ${receiveDate}
#Execute Javascript window.scrollTo(0,400)
Sleep 3
${value}= Get Element Attribute ${receiveDate} attribute=value

${receive_date}= Convert Date ${value} result_format= ‘%m/%d/%y, %I:%M:%S %p’

 ${date_receive}=  Convert Date   ${value}  epoch
 #${date_receive}=  Convert DATE    ${value}




   Log    Receive Date ${date_receive}

#Get Text ${value}

    Log To Console  Receive Date ${date_receive}

#${receiveDates}= Convert Date ${receiveDate} date_format=%M-%d-%yyyy %H:%M:%S

Regulatory Due Date field value should be present
Sleep 3
Scroll Element Into View ${regulatory_DueDate}
Sleep 2
${regulatory}= Get Element Attribute ${regulatory_DueDate} attribute=value

${regDate}=  Convert Date  ${regulatory}  epoch
#${regulatory_due_date}=  Convert Date    ${regulatory_Due}  result_format= '%m/%d/%y, %I:%M:%S %p'

Log To Console  Regulatory DueDate ${regDate}
Log  Regulatory DueDate ${regDate}

#Get Text ${regulatory_DueDate
TAT For Standard

${diff}= Evaluate ${regDate} - ${date_receive}
${diff1}= Evaluate ${diff} /24*3600
Log ${diff}
Should Be True ${diff}==30

This is the code I have written still , I’m not getting the desired result.
Getting below Error.
Please rectify my code to get desired result.

Date in string format:
…Receive Date 01-13-2022 09:22:16 PM
.Regulatory DueDate 02-12-2022 09:22:16 PM
Difference I need to get.

ValueError: time data ‘0114-20-22 01:00:11.000000’ does not match format ‘%Y-%m-%d %H:%M:%S.%f’

Many Thanks.

Hi Damies ,
This is another approach i tried but with no luck.
imported python file as library in to robot class.Below details of my two files.
still figuring out where i’m missing.

convesion.py

from datetime import datetime


class DateConversion:

    def convertDATE(date_time):
        format = '%d-%m-%Y %I:%M:%S %p'  # The format
        datetime_str = datetime.strptime(date_time, format)
        print(datetime_str)
        return datetime_str
    #convertDATE("01-02-2021 06:23:45 PM")

Robot class

*** Settings ***
Documentation    This Suite is to test the create incident functionality.
Library    SeleniumLibrary
Library    String
Library    DateTime
Variables  ../WebElements.py
Resource  ../../Properties/config.robot
Library  ../DateConversion.py


*** Variables ***



*** Keywords ***

go to +incident icon on homepage and click on it

     Wait Until Element Is Visible    ${incident_icon}
     Click Element  ${incident_icon}


Choose Medicare Incident from dropdown list
     Wait Until Element Is Visible    ${medicare_incident}
     Click Element    ${medicare_incident}

Create Incident Modal form should be visible
     Wait Until Element Is Visible  ${modal_box}
     Get Text  ${modal_box}
     Element Text Should Be    ${modal_box}    ${modal_box_title}
MemberId Field Validation Via +IconSearchBox
   Wait Until Element Is Visible  ${Icon_searchMemebr_plusIcon}
   Input Text    ${enterTag}    ${member}
   Click Element    ${Icon_searchMemebr_plusIcon}
   Get Text    ${memberid_field}
   Log    ${memberid_field}
   Log To Console    ${memberid_field}
   Element Text Should Be    ${memberid_field}    ${member}
   Sleep  1

Incident webform MemberId field validation via FunnelIcon
  Click Element    ${MemberId_serachIcon}
  Wait Until Element Is Visible    ${memberid}
  Input Text    ${memberid}    ${member2}
  Wait Until Element Is Visible    ${search_button}
  Click Element    ${search_button}
  Wait Until Element Is Visible    ${Member_selection_checkBox}
  Click Element    ${Member_selection_checkBox}
  Wait Until Element Is Visible    ${confirmation}
  Set Selenium Speed    ${DELAY}
  #Wait Until Keyword Succeeds    5    0.5 ${confirmation}
  Click Element    ${confirmation}
  Sleep  1
  Get Text    ${memberid_field}
  Log  ${memberid_field}
  Log To Console    ${memberid_field}
  Element Text Should Be    ${memberid_field}    ${member2}
  
 Grievance description field validation
  Sleep    3
  Execute Javascript  window.scrollTo(0,1700)
  Input Text    ${grievance_detail_desc}  ${LETTERS}
 Case short Description Field validation
   Sleep  3
   #Execute Javascript  window.scrollTo(0,300)
   Input Text    ${caseShortDescription}    ${caseShortDescriptionValue}
Receive Date field value should be present
  Sleep  3
  Scroll Element Into View    ${receiveDate}
    #Execute Javascript  window.scrollTo(0,400)
    Sleep  3
    ${value}=  Get Element Attribute    ${receiveDate}    attribute=value
   # ${receive_date}=  Convert Date  ${value}  result_format= '%m/%d/%y, %I:%M:%S %p'
     ${date_receive}=  Convert DATE   ${value}
     #${date_receive}=  Convert DATE    ${value}




       Log    Receive Date ${date_receive}
   #Get Text    ${value}

        Log To Console  Receive Date ${date_receive}



   #${receiveDates}=  Convert Date    ${receiveDate}  date_format=%M-%d-%yyyy %H:%M:%S

 Regulatory Due Date field value should be present
    Sleep  3
    Scroll Element Into View  ${regulatory_DueDate}
    Sleep  2
     ${regulatory}=  Get Element Attribute    ${regulatory_DueDate}    attribute=value

    ${reg_date}=  Convert DATE  ${regulatory}
    #${regulatory_due_date}=  Convert Date    ${regulatory_Due}  result_format= '%m/%d/%y, %I:%M:%S %p'

    Log To Console  Regulatory DueDate ${reg_date}
    Log  Regulatory DueDate ${reg_date}

   #Get Text    ${regulatory_DueDate
 TAT For Standard

   ${diff}=  Subtract Date From Date    ${reg_date}    ${date_receive}

    Log  ${diff}
        Should Be True    ${diff}==30



Getting following error:

Keyword ‘DateConversion.Convert DATE’ expected 0 arguments, got 1.

I’m new to robot framework , Please help.

@akshay

You problem is the python code.
You implemented a function of a class.
As this function has no decorator to be static, the first argument is always self, which is the instance of that class itself.

add “self” to the method

from datetime import datetime


class DateConversion:

    def convertDATE(self, date_time):
        format = '%d-%m-%Y %I:%M:%S %p'  # The format
        datetime_str = datetime.strptime(date_time, format)
        print(datetime_str)
        return datetime_str

or make the method a "static method"

from datetime import datetime


class DateConversion:

    @staticmethod
    def convertDATE(date_time):
        format = '%d-%m-%Y %I:%M:%S %p'  # The format
        datetime_str = datetime.strptime(date_time, format)
        print(datetime_str)
        return datetime_str

Hi @akshay ,

Your example got messed up, so I couldn’t really follow what you were doing but it looks overly complicated compared to the problem from your original post, here is a few working example of what I had in mind:

*** Variables ***

# format "mm-dd-yyyy hh:mm:ss AM/PM "
${DateFormat}		%m-%d-%Y %I:%M:%S %p
# …Receive Date 01-13-2022 09:22:16 PM
${ReceiveDate}		01-13-2022 09:22:16 PM
${ReceiveLate}		02-13-2022 09:22:16 PM
# .Regulatory DueDate 02-12-2022 09:22:16 PM
${DueDate}		02-12-2022 09:22:16 PM

*** Test Cases ***
Compare Dates
	# ${items} =	Evaluate	[json.loads(item) for item in ('1', '"b"')]	modules=json
	${epoch_r}= 	Evaluate    datetime.datetime.strptime("${ReceiveDate}", "${DateFormat}").timestamp()		modules=datetime
	${epoch_d}= 	Evaluate    datetime.datetime.strptime("${DueDate}", "${DateFormat}").timestamp()		modules=datetime
	${difference}=	Evaluate	${epoch_d} - ${epoch_r}

Keyword Compare
	[Documentation]		Might be usefull if you need to compare lots of dates
	${isbefore}=		Date Diff		${DueDate}		${ReceiveDate}
	Should Be True		${isbefore} > 0

Fail Example
	${isbefore}=		Date Diff		${DueDate}		${ReceiveLate}
	Should Be True		${isbefore} > 0

*** Keywords ***
Date Diff
	[Arguments]		${DueDate}	${ReceiveDate}
	${epoch_r}= 	Evaluate    datetime.datetime.strptime("${ReceiveDate}", "${DateFormat}").timestamp()		modules=datetime
	${epoch_d}= 	Evaluate    datetime.datetime.strptime("${DueDate}", "${DateFormat}").timestamp()		modules=datetime
	${difference}=	Evaluate	${epoch_d} - ${epoch_r}
	[return]  ${difference}

You really only need the variables and the Compare Dates test case, the rest is to show you what I would do with a keyword for comparing dates.

Hope that helps,

Dave.

Thank you All for quick response. As that was my early stage of robot framework so this small issue i made it big. Now i 'm quite familiar with the framework and architecture.

Quick help radio button how to validate in robot framework.
I have a scenario when i click on a radio button the dependent radio buttons will become ‘NA’ and become disable fields sametime. How to validate those disable fields radio button.

for that i captured the xpath by python code with parent div and label text but how to validate the selected radio in robot framework.
suppose Y, N ,NA 3 radio buttons are avilable in noneditable state.
but NA is selected so I want to validate that NA is selected .How to perform that operation.

Many Thanks.

Hi Akshay,

To check the Radio Button is set to NA you probably want Radio Button Should Be Set To though Get Value might be useful too

As for checking the other form fields you probably want Get Element Attribute or Element Attribute Value Should Be and you’ll probably want to check the element for an attribute disabled or hidden

Dave.

Hi Damies,
[Radio Button Should Be Set To method takes one parameter as groupid . which now a days in development noone use .My concern here is not to get the disable field values but to get the selected radio button 's value.Which is not happening or for any radio button validation . It’s asking for the group id as one parameter . Which i belief developer don’t use now a days any common groupid .How to manage radio button validation effective way in robot framework without use of group id . Any help on this would be greatful.

Many Thanks.

Regards,
Akshay

how to convert ```
11/28/2022 12:00 PM IST these time in '%d-%m-%Y %I:%M:%S.%f %p %Z

Hi Pratik,

That’s easily achieved by combining the BuiltIn Evaluate and python’s DateTime.strptime

It would look something like this:

${intime}=    Set Variable    11/28/2022 12:00 PM IST
${dtobj}=    Evaluate    DateTime.strptime("${intime}", "%d-%m-%Y %I:%M:%S.%f %p %Z")    DateTime

Dave.