[英]How to convert string date to a epoch timestamp in python
我有一個字符串日期為2015-03-25T00:00:00Z
。 如何將其轉換為Unix紀元1426636800000.0
python中是否有任何庫可以做到這一點。
time.strptime(string [,format])
import time
print time.strptime("2015-03-25T00:00:00Z","%Y-%m-%dT%H:%M:%SZ")
例如,使用時間。
因此,首先需要將字符串轉換為時間對象(或者您可以使用datetime或halex),然后獲取自紀元以來的秒數。
>>> import time
>>> time.mktime(time.strptime('2015-03-25T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ'))
1427241600.0
如果您擁有Python 3.3或更高版本,則可以使用datetime模塊:
>>> import datetime
>>> datetime.datetime.strptime("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ").timestamp()
1427238000.0
您可以使用easy_date使其變得容易:
import date_converter
timestamp = date_converter.string_to_timestamp("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.