Python: Converting string to timestamp with microseconds

前端 未结 3 1804
南旧
南旧 2020-12-31 10:32

I would like to convert string date format to timestamp with microseconds I try the following but not giving expected result:

\"\"\"input string date ->          


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 11:26

    In Python 3.4 and later you can use

    timestamp = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timestamp()
    

    This doesn't require importing the time module. It also uses less steps so it should be faster. For older versions of python the other provided answers are probably your best option.

    However, the resulting timestamp will interpret myDate in local time, rather than UTC, which may cause issues if myDate was given in UTC

提交回复
热议问题