在Python中将ISO 8601日期时间转换为秒 [英] Converting ISO 8601 date time to seconds in Python
本文介绍了在Python中将ISO 8601日期时间转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试相加两次. ISO8601时间戳为'1984-06-02T19:05:00.000Z',我想将其转换为秒.我尝试使用Python模块 iso8601
,但这只是一个解析器.
I am trying to add two times together. The ISO 8601 time stamp is '1984-06-02T19:05:00.000Z', and I would like to convert it to seconds. I tried using the Python module iso8601
, but it is only a parser.
有什么建议吗?
推荐答案
如果要获取自纪元以来的秒数,可以使用python-dateutil
将其转换为datetime
对象,然后将其转换为秒数. strftime
方法.像这样:
If you want to get the seconds since epoch, you can use python-dateutil
to convert it to a datetime
object and then convert it so seconds using the strftime
method. Like so:
>>> import dateutil.parser as dp
>>> t = '1984-06-02T19:05:00.000Z'
>>> parsed_t = dp.parse(t)
>>> t_in_seconds = parsed_t.strftime('%s')
>>> t_in_seconds
'455047500'
所以你到了一半:)
这篇关于在Python中将ISO 8601日期时间转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文