受到Unix时间戳的启发,我发现时间转成秒数后会非常好处理,在程序当中不再是以字符串的形式处理,不管时间的加减还是获取随机的时间点都变得非常方便,

如果有需要,也很容易转换成需要的时间格式。

一:时间转成秒数

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时间时分秒与秒数的互相转换的更多相关文章

  1. js 时分秒与秒数的转换

    1. 时间戳 格式化为 时分秒(00:00:00) /** * 时间秒数格式化 * @param s 时间戳(单位:秒) * @returns {*} 格式化后的时分秒 */ var sec_to_t ...

  2. 获取sql 时间时分秒

    select DATE_FORMAT(now(),'%Y-%m-%d %T') from dual;  年月日时分秒 select DATE_FORMAT(now(),'%T') from dual; ...

  3. python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。

    具体代码如下> import paramiko,re,datetime,time ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy ...

  4. sql获取时间时分秒

    select datename(hour,getdate())+':'+datename(minute,getdate())+':'+datename(second,getdate())

  5. VUE环境下获取当前时间并格式化--按秒数更新

    <el-col :span="8"><div class="grid-content title-time"> {{date}}< ...

  6. mktime(将时间结构数据转换成经过的秒数)

    mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime() ...

  7. MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

    MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...

  8. Java 获取当前时间距离当天凌晨的秒数

    原文出自:https://blog.csdn.net/seesun2012 在前期项目中遇到一个客户端与服务器间的时间同步问题,需要获取到当前时间与当天凌晨时间距离的秒数,写这篇文章主要是为了总结一下 ...

  9. C#_计算目前时间到指定的周X、指定的时间X 还有多少秒

    比如:当前时间到下周二 05:00:00还剩下多少秒? /// <summary> /// 计算距离下一个 周XX XX时XX分XX秒,还剩下多少秒 /// </summary> ...

随机推荐

  1. PBOC~PPT-补充内容B(转)

    PBOC电子现金基于借记/贷记应用上小额支付的一种实现.在借记卡上可以解释为预付,在贷记卡上可以解释为预先授权.预付的金额或预授权额度在卡片中体现为可脱机消费的金额,也就是电子现金余额.电子现金解决方 ...

  2. loadrunner json

    Action(){ web_custom_request("JRPT_WriteLog", //VuGen中树形视图中显示的名称 "Url=url", //请求 ...

  3. UIautomator Python测试

    #!/usr/bin/env python # -*- coding: utf-8 -*- import unittest from mock import MagicMock, patch impo ...

  4. C#基础

    .net/dotnet:一般指.NetFramework框架,一种平台,一种技术. c#(sharp):一种编程语言,可以开发基于.net平台的应用. Java:是一种技术,又是一门语言: .net应 ...

  5. java怎么定义一个二维数组?

    java中使用 [][] 来定义二维数组 定义数组时也可同时初始化下面是一些例子float[][] numthree; //定义一个float类型的2维数组numthree=new float[5][ ...

  6. office2003安装公式编辑器mathtype5.2

    同事的一台电脑,xp系统,需要安装公式编辑器mathtype,安装完后,启动word时出现了宏的警告. 在菜单中,无论如何设置宏,都不能去除该提示框.删除了模板normal.dot,也不能解决该问题. ...

  7. mongodb的一些小总结

    mongodb的安装,官网下载想要的版本,可视化工具mongovue(注意不支持mongodb3.0以上的版本) 下载mis安装,解压后bin,... 1.配置环境变量,将H:\mongodb\mon ...

  8. org.apache.catalina.webresources.Cache.getResource Unable to add the resource

    org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx to the cache be ...

  9. easyui的textbox赋值小结

    使用的系统中有个后台,需要填充单号,如下图: 每次往框里面填充都是一样的数据,复制.粘贴,而且当人数颇多的时候,就是体力活. 于是就想到通过执行js代码,自动填充这些数据. chrome下F12,查看 ...

  10. toolsqa-IMPORTANCE

    http://toolsqa.com/cucumber/cucumber-options/