python时间时分秒与秒数的互相转换
2016-12-10 03:05
309 查看
受到Unix时间戳的启发,我发现时间转成秒数后会非常好处理,在程序当中不再是以字符串的形式处理,不管时间的加减还是获取随机的时间点都变得非常方便,
如果有需要,也很容易转换成需要的时间格式。
stackoverflow.com上还有更多的写法,有兴趣可以自己去看。当然方法一最简单明了,直接用这样的方法是最好的。
http://stackoverflow.com/questions/10663720/converting-a-time-string-to-seconds-in-python
http://stackoverflow.com/questions/6402812/how-to-convert-an-hmmss-time-string-to-seconds-in-python
http://stackoverflow.com/questions/775049/python-time-seconds-to-hms
下篇再写转成时间转成秒数后能用来干嘛。
2016-12-10 0:21:56 codegay
如果有需要,也很容易转换成需要的时间格式。
一:时间转成秒数
st = "08:30:30" et = "9:33:33" #方法一 def t2s(t): h,m,s = t.strip().split(":") return int(h) * 3600 + int(m) * 60 + int(s) print(t2s(st)) #方法二 import datetime var = ("hours","minutes","seconds") time2sec = lambda x:int(datetime.timedelta(**{k:int(v) for k,v in zip(var,x.strip().split(":"))}).total_seconds()) print(time2sec(st))
stackoverflow.com上还有更多的写法,有兴趣可以自己去看。当然方法一最简单明了,直接用这样的方法是最好的。
http://stackoverflow.com/questions/10663720/converting-a-time-string-to-seconds-in-python
http://stackoverflow.com/questions/6402812/how-to-convert-an-hmmss-time-string-to-seconds-in-python
二:秒数转成时分秒:
下面的方法是从stackoverflow上抄过来的。http://stackoverflow.com/questions/775049/python-time-seconds-to-hms
m, s = divmod(seconds, 60) h, m = divmod(m, 60) print ("%02d:%02d:%02d" % (h, m, s))
下篇再写转成时间转成秒数后能用来干嘛。
2016-12-10 0:21:56 codegay
相关文章推荐
- python时间时分秒与秒数的互相转换
- Shell Python 日期和时间戳的互相转换
- Python时间戳与时间字符串互相转换
- Python时间戳与时间字符串互相转换实例代码
- python 爬虫中时间和时间戳的互相转换
- Python时间戳与时间字符串互相转换实例代码
- Python时间和时间戳互相转换
- P006_Python时间戳与时间字符串互相转换
- Python时间戳与时间字符串互相转换实例代码
- Python时间戳与时间字符串互相转换实例代码
- Unix 时间戳 互相转换 系统标准形式时间
- python:从任意格式的时间转换为Unix时间戳(Unix timestamp)
- python中列表,元组,字符串如何互相转换
- python3中bytes与string的互相转换
- 日期和unix时间戳的互相转换
- python日期时间字符串转换成unix时间戳
- python的时间戳转换
- python 字符串和时间转换
- Python中时间戳与时间字符串互相转化
- Unix时间戳转换(python)