如何将时间转换为时间戳?

时间:2016-01-11 03:14:12

标签: python datetime python-3.x time timestamp

这样的时间字符串:'Thu Nov 26 17:49:28 +0000 2015'

需要将其转换为UTC时间戳,尝试从在线找到的几种方法,但无法正常工作。

转换它的正确方法是什么? THX。

3 个答案:

答案 0 :(得分:0)

datetime.datetime.strptime()与正确的格式说明符一起使用,然后使用timestamp()方法:

>>> import datetime
>>> d = 'Thu Nov 26 17:49:28 +0000 2015'
>>> datetime.datetime.strptime(d, '%a %b %d %H:%M:%S %z %Y').timestamp()
1448560168.0

答案 1 :(得分:0)

rfc 5322 date-time string获取“自纪元以来的秒数”(如电子邮件if (offset <= 1352) { $('#scrollup table').css("top", 0); $('#scrollup table tr:last').after($('#scrollup table tbody tr:first').detach()); //tbody here } 标题中所示):

Date

答案 2 :(得分:0)

如果你喜欢熊猫,这很简单

In [32]: import pandas as pd
# Construct date time index
In [33]: dt_index = pd.DatetimeIndex(['Thu Nov 26 17:49:28 +0000 2015'])
# Get the epoch timestamp in seconds
In [35]: dt_index.astype('int64')/1e9
Out[35]: array([  1.44856017e+09])

In [36]: dt_index.astype('int64')[0]/1e9
Out[36]: 1448560168.0