首页 > 解决方案 > 使用Robot框架评估后如何使用0获取月份格式

问题描述

我尝试在评估月份后获得像“06/19”这样的月份和年份格式,但我得到的只是“6/19”。

Month and year
   ${currentYear}=    Get Current Date  result_format=%y
   ${currentDate}=    Get Current Date
   ${datetime} =  Convert Date  ${currentDate}    datetime
   ${getMonth}=   evaluate   ${datetime.month} - 1
   log to console   ${getMonth}/${currentYear}

我已经通过创建变量 @{MONTHSNO} ${EMPTY} 01 02 03 04 05 06 07 08 09 10 11 12 尝试了另一种方法并返回 ${MONTHSNO}[${getMonth}]/${currentYear} 我得到了 06/19但我不确定机器人是否有另一种方法可以将月份转换为“06”,而无需制作像这样的变量。

标签: robotframework

解决方案


当您运行 Evaluate 命令时,您正在运行 python 命令。因此,让我们看一下 datetime 文档:

https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

他们最后有这一部分说图书馆时间很有用。所以我建议你使用这个命令以 0X 格式返回月份:

${getMonth}=   evaluate   time.strftime("%m")

这只是把 07 还给我(因为现在是七月)


推荐阅读