How do you remove seconds and milliseconds from a date time string in python

后端 未结 2 506
小鲜肉
小鲜肉 2021-01-18 17:33

How I can convert a date in format \"2013-03-15 05:14:51.327\" to \"2013-03-15 05:14\", i.e. removing the seconds and milliseconds. I don\'t think

相关标签:
2条回答
  • 2021-01-18 17:57

    Try this (Thanks Blender!)

    >>> date = "2013-03-15 05:14:51.327"
    >>> newdate = date.rpartition(':')[0]
    >>> print newdate
    2013-03-15 05:14
    
    0 讨论(0)
  • 2021-01-18 18:02

    In Robotframework the most straightforward way would be to user Split String From Right from the String library 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
    
    0 讨论(0)
提交回复
热议问题