How to validate two date with in 24 hours

i am new at robot framework
want to validate two 28/12/2022 and 29/12/2022 with in 24 hour

29/11/2022 12:00 PM IST’ actual time fetch from ui ${form1}

     ${from}  Get Text  ${locator}
     ${To}  Get Text  ${locator}
     ${from1} =	Convert Date  ${from}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M: %p
     ${To1} =	Convert Date  ${To}  exclude_millis=yes	 date_format=%d/%m/%Y %I:%M: %p
     ${Subtract_Time1}  Subtract Date From Date  ${To1}  ${from1}
     Log To Console  ${Subtract_Time1}

after that I get
time data ‘29/11/2022 12:00 PM IST’ does not match format ‘%d/%m/%Y %I:%M: %p’

Hi Pratik,

Save this as rahul12.robot and run it, it should help you understand where your issues were.

Dave.

rahul12.robot

*** Settings ***
Library		DateTime

*** Variables ***

${badfrom}		29/11/2022 12:00 PM IST
${from}		28/11/2022 12:00 PM
${To}		29/11/2022 12:00 PM
${ToLong}		29/11/2022 12:02 PM

*** Test Cases ***
rahul12 Bad Parse 1
	Log    This will faile because of the : after the %M
	${from1} =	Convert Date  ${from}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M: %p

rahul12 Bad Parse 2
	Log    This will faile because of the IST
	${from1} =	Convert Date  ${badfrom}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M %p

rahul12 Date Time Test
	${from1} =	Convert Date  ${from}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M %p
	${To1} =	Convert Date  ${To}  exclude_millis=yes	 date_format=%d/%m/%Y %I:%M %p
	${Subtract_Time1}  Subtract Date From Date  ${To1}  ${from1}
	Log To Console  ${Subtract_Time1}
	Should Be True    ${Subtract_Time1} <= 86400

rahul12 Date Time Test Long
	Log    This will faile because it's more than 24 hours (86400 sec)
	${from1} =	Convert Date  ${from}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M %p
	${To1} =	Convert Date  ${ToLong}  exclude_millis=yes	 date_format=%d/%m/%Y %I:%M %p
	${Subtract_Time1}  Subtract Date From Date  ${To1}  ${from1}
	Log To Console  ${Subtract_Time1}
	Should Be True    ${Subtract_Time1} <= 86400

Also you cane use String Library’s Replace String to remove the IST if you need to

${from}=	Replace String		${badfrom}		IST		${EMPTY}

Thanks damies13

     ${Var_from}  Get Text  ${locator}
     ${Var_To}  Get Text  ${locator}
     ${from} =  Replace String  ${Var_from}  IST  ${EMPTY}
     ${from1} =	 Convert Date  ${from}  exclude_millis=yes	date_format=%d/%m/%Y %I:%M %p
     ${To} =  Replace String  ${Var_To}  IST  ${EMPTY}
  ${To1} =  Convert Date  ${To}  exclude_millis=yes  date_format=%d/%m/%Y %I:%M %p
  ${Subtract_Time1}  Subtract Date From Date  ${To1}  ${from1}
   Log To Console  ${Subtract_Time1}
   Should Be True    ${Subtract_Time1} <= 86400

after that I get Value Error: unconverted data remains
how deal with these

Hi Pratik,

You might need to do this:

${from}=	Replace String		${badfrom}		${SPACE}IST		${EMPTY}

as the space after PM and before IST might also be causing you an issue

Dave.

Hi

I’m trying to get 2 values from db which is date and trying to subtract number of days in between

Pblm is
One query result comes as datetime. date
Other query results comes as datetime.datetime

Convert to date keyword in robot framework has pblm with datetime.date
It says unsupported input format.

Can you guys reply how to convert this datetime.date to datetime.datetime using robot?

Thnks for ur help

Hi @sethu90,

Welcome to the community, it’s usually best practice to create a new issue when you have a different issue or the issue your posting in (like this one) is long ago dormant.

A quick question, do you mean that your database servers are returning python objects that are of those types or text strings that look like those types?

Most databases actually store date time values as an interger (usually milliseconds since epoch or similar), so they easiest solution might be to modify the database query to return the date time in the same format, or even better get them both to return seconds since epoch as that will make the calculations much easier.

If they are strings then you’ll probably need to use strftime() and strptime() to make them the same format

If they are python objects then you might be able to do something like
datetime.fromisoformat(datetime.date.isoformat()).datetime

Hopefully you’ll find your solution somewhere in there.

Dave.

1 Like

Hi sethu90

You’d most likely get more help raising a new post for your query so it doesnt get lost by OP’s question.

But please see the below example:

*** Settings ***
Library     DateTime

*** Test Cases ***
Example Date Time  
    ${example_datetime_date}=   Set Variable    2024-04-21
    ${output_example_datetime_date}=    Convert Date    ${example_datetime_date}     date_format=%Y-%m-%d
    Log To Console    \n${output_example_datetime_date}

    ${example_datetime_datetime}=   Set Variable    2024-04-24 15:45:21
    ${output_example_datetime_datetime}=    Convert Date    ${example_datetime_datetime}     date_format=%Y-%m-%d %H:%M:%S     
    Log To Console    ${output_example_datetime_datetime}

    ${difference_in_days}=    Subtract Date from Date    ${output_example_datetime_datetime}    ${output_example_datetime_date}    verbose
    Log To Console   ${difference_in_days}

Link to working example - Never tried this before, so hopefully it links through ok.

Please see library for details DateTime

The format error will be this part you’d need to set: date_format=%Y-%m-%d and hopefully the example above helps.

Thanks

EDIT: It looks as though myself and @damies13 posted at the same time :slight_smile:

1 Like