How to convert python timestamp string to epoch?

前端 未结 6 1222
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 17:53

I have the following string:

mytime = \"2009-03-08T00:27:31.807Z\"

How do I convert it to epoch in python?

I tried:



        
6条回答
  •  遥遥无期
    2020-12-28 18:10

    dateutil is the only library i have found that correctly deals with the timezone offset identitifier (Z)

    pip install python-dateutil
    

    then

    from dateutil.parser import parse as date_parse
    print date_parse("2009-03-08T00:27:31.807Z")
    #get timestamp
    
    import calendar
    dt =  date_parse("2009-03-08T00:27:31.807Z")
    timestamp1 = calendar.timegm(dt.timetuple())
    

提交回复
热议问题