python获取当前时间的时间戳,python 当前时间戳

  python获取当前时间的时间戳,python 当前时间戳

  时间戳是指从1970年1月1日(00:00:00 GMT)到当前时间的总秒数。它也被称为Unix时间戳。

  在python中,时间戳是一串数字。当我们想将其转换成某种格式的时间数据,比如‘2018-08-08 11:11:11’,就需要使用时间模块。同样,时间数据也可以转换成时间戳。

  python中的时间戳计算:

  1.将字符串的时间转换成时间戳importtime。

  a=2013-10-1023:40:00

  #将其转换为时间数组

  timeArray=time.strptime(a, %Y-%m-%d%H:%M:%S )

  #转换为时间戳

  timeStamp=int(time . mktime(time array))

  时间戳==1381419600

  2.时间戳被转换成指定的格式日期:

  方法1:

  使用localtime()将其转换为时间数组,然后将其格式化为所需的格式,比如importtime。

  时间戳=1381419600

  timeArray=time.localtime(时间戳)

  otherStyleTime=time . strftime( % Y-% M-% d % H:% M:% S ,timeArray)

  other style time== 2013-10-1023:40:00

  方法2:导入日期时间

  时间戳=1381419600

  date array=datetime . datetime . utcfromtimestamp(时间戳)

  other style time=date array . strftime( % Y-% M-% d % H:% M:% S )

  other style time== 2013-10-1023:40:00

  更多Python知识,请关注Python自学网。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: