vinu
vinu

Reputation: 191

how to subtract time from date if date is coming as a string using robot framework

I am trying to subtract time from a date which is in string format however when i run the below code it gives me error. time data '0301-20-17 00:00:00.000000' does not match format '%Y-%m-%d %H:%M:%S.%f'. Below is the code

${Mydate}=  Set Variable    03/01/2017      
${MyNewDate}=   Subtract Time From Date  ${Mydate}  2d  result_format=%m/%d/%Y
Log  ${MyNewDate}

Please help with this.

Upvotes: 0

Views: 5202

Answers (3)

Mirela Lou
Mirela Lou

Reputation: 1

error "does not match format '%Y-%m-%d %H:%M:%S.%f", says that you need to have your date according to this format. Then you can have whatever custom Timespan you want.

${Mydate}=      Set Variable    2017/03/01   
${MyNewDate}=   Subtract Time From Date  ${Mydate}  2 days  result_format=%m/%d/%Y
Log  ${MyNewDate}

Upvotes: 0

Goralight
Goralight

Reputation: 2107

Building on Vivi's answer - Without needing to follow the format governed by Robot which is already in place. You need to define the Date_format also.

Below worked for me:

    ${Mydate}    Set Variable    03/01/2017
    ${MyNewDate}=    Subtract Time From Date    ${Mydate}    2d    result_format=%m/%d/%Y    exclude_millis=True    date_format=%m/%d/%Y
    Log    ${MyNewDate}

Upvotes: 2

Vivi
Vivi

Reputation: 431

Try with Format as "2017-03-01 00:00:00.00":

${Mydate}   Set Variable   "2017-03-01 00:00:00.00"
${MyNewDate}=   Subtract Time From Date    ${Mydate}     2d   result_format=%m/%d/%Y
log    ${MyNewDate}

Upvotes: 1

Related Questions