string to datetime with fractional seconds, on Google App Engine

前端 未结 2 1574
无人及你
无人及你 2021-02-07 11:28

I need to convert a string to a datetime object, along with the fractional seconds. I\'m running into various problems.

Normally, i would do:

>>>         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-07 12:15

    All ancient history by now, but in these modern times you can also conveniently use dateutil

    from dateutil import parser as DUp
    
    funky_time_str = "1/1/2011 12:51:00.0123 AM"
    
    foo = DUp.parse(funky_time_str)
    
    print foo.timetuple()
    # time.struct_time(tm_year=2011, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=51, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)
    
    print foo.microsecond
    # 12300
    
    print foo
    # 2011-01-01 00:51:00.012300
    

    dateutil supports a surprising variety of possible input formats, which it parses without pattern strings.

提交回复
热议问题