如何从python中的日期时间字符串中删除秒和毫秒

时间:2013-03-15 09:48:12

标签: python-2.7 selenium-webdriver robotframework

如何将"2013-03-15 05:14:51.327"格式的日期转换为"2013-03-15 05:14",即删除秒和毫秒。我认为机器人框架工作没有办法。如果有人在python中有解决方案,请告诉我。

2 个答案:

答案 0 :(得分:4)

试试这个(感谢Blender!)

>>> date = "2013-03-15 05:14:51.327"
>>> newdate = date.rpartition(':')[0]
>>> print newdate
2013-03-15 05:14

答案 1 :(得分:0)

在Robotframework中,最直接的方法是访问用户Split String From Right from the String library库:

${datestring}=    Set Variable    2019-03-15 05:14:51.327
${parts}=  Split String From Right    ${datestring}    :    max_split=1
# parts is a list of two elements - everything before the last ":", and everything after it
# take the 1st element, it is what we're after
${no seconds}=    Get From List     ${parts}    0
Log    ${no senods}    # 2019-03-15 05:14