You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Robot Framework中从日期时间字符串移除时间部分

解决Robot Framework提取日期、移除时间部分的问题

你当前获取到的设备安装日期字符串是09/06/2023 13:14,想要保留日期、移除时间部分。之前用remove string方法出错,是因为这个关键字只会删除指定的单个字符(空格和冒号),而非移除整个时间段,所以才会得到09/06/20231314这种结果。

以下是几种正确的实现方式:

方法1:按空格分割字符串(最简便)

利用日期和时间之间的空格做分隔,拆分后取第一个元素即可:

${date_time_list}=    Split String    ${get_deviceinstalldate}    ${SPACE}
${only_date}=    Set Variable    ${date_time_list}[0]
log to console    Device Install Date:${only_date}

方法2:截取子字符串

先找到空格的位置,再截取该位置之前的内容:

${space_index}=    Get Index    ${get_deviceinstalldate}    ${SPACE}
${only_date}=    Get Substring    ${get_deviceinstalldate}    0    ${space_index}
log to console    Device Install Date:${only_date}

方法3:正则表达式提取

如果日期格式固定,用正则匹配提取日期部分:

${only_date}=    Evaluate    re.search(r'^\d{2}/\d{2}/\d{4}', '${get_deviceinstalldate}').group()    modules=re
log to console    Device Install Date:${only_date}

内容的提问来源于stack exchange,提问作者lng60044

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:34:58