python——时间与时间戳之间的转换

墙裂推荐:datetime——廖雪峰

注:常用的时间转换代码

# 一、将时间戳转换成时间
>>> import time
>>> st = time.localtime(1555638331)
>>> ft = time.strftime('%Y-%m-%d %H:%M:%S', st)
>>> ft
'2019-04-19 09:45:31'

# 二、将特殊时间转化成时间
>>> import datetime
>>> import time
>>> GMT_time = datetime.datetime.strptime("2019-04-24T13:39:42Z", "%Y-%m-%dT%H:%M:%SZ")
# GMT_time:格林尼治时间
# GMT_time == datetime.datetime(2019, 4, 24, 13, 39, 42)
>>> LOCAL_time = GMT_time + datetime.timedelta(hours=8)
# LOCAL_time:北京时间
# LOCAL_time == datetime.datetime(2019, 4, 24, 21, 39, 42)
>>> local_time = LOCAL_time.strftime("%Y-%m-%d %H:%M:%S")
>>> local_time
'2019-04-24 21:39:42'

对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种:

  • 将时间转换为时间戳
  • 重新格式化时间
  • 时间戳转换为时间
  • 获取当前时间及将其转换成时间戳

1、将时间转换成时间戳

将如上的时间2016-05-05 20:28:54转换成时间戳,具体的操作过程为:

  • 利用strptime()函数将时间转换成时间数组
  • 利用mktime()函数将时间数组转换成时间戳
#coding:UTF-8
import time

dt = "2016-05-05 20:28:54"

#转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
#转换成时间戳
timestamp = time.mktime(timeArray)

print timestamp
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、重新格式化时间

重新格式化时间需要以下的两个步骤:

  • 利用strptime()函数将时间转换成时间数组
  • 利用strftime()函数重新格式化时间
#coding:UTF-8
import time

dt = "2016-05-05 20:28:54"

#转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
#转换成新的时间格式(20160505-20:28:54)
dt_new = time.strftime("%Y%m%d-%H:%M:%S",timeArray)

print dt_new

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3、将时间戳转换成时间

在时间戳转换成时间中,首先需要将时间戳转换成localtime,再转换成时间的具体格式:

  • 利用localtime()函数将时间戳转化成localtime的格式
  • 利用strftime()函数重新格式化时间
#coding:UTF-8
import time

timestamp = 1462451334

#转换成localtime
time_local = time.localtime(timestamp)
#转换成新的时间格式(2016-05-05 20:28:54)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

print dt

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4、按指定的格式获取当前时间

利用time()获取当前时间,再利用localtime()函数转换为localtime,最后利用strftime()函数重新格式化时间。

#coding:UTF-8
import time

#获取当前时间
time_now = int(time.time())
#转换成localtime
time_local = time.localtime(time_now)
#转换成新的时间格式(2016-05-09 18:59:20)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

print dt
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

参考:

  1. python——时间与时间戳之间的转换
  2. Python3 日期和时间
  3. Python 如何优雅的将数字转化为时间格式
  4. python时间,日期,时间戳处理
  5. Python的time(时间戳与时间字符串互相转化)
  6. Python 时间戳和日期相互转换
  7. Python 时间戳和日期相互转换
  8. python——时间与时间戳之间的转换
  9. python之时间和时间戳转换
  10. Python 时间戳与时间字符串互相转
  11. Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳
  12. 关于时间格式 2016-08-9T10:01:54.123Z 20160809100154.123Z 处理方法
  13. Python之日期与时间处理模块(date和datetime)
  14. Python 日期时间处理模块学习笔记
  15. python中字符串与datetime的相互转换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dev_zyx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值