datetime, 时间戳,字符串,time,各种时间格式的转换

1. 获得当前时间的时间戳

获得当前时间的时间戳有两种方法

import time
from datetime import datetime

# 获得当前时间戳
current_time = int(time.time())
print(current_time)

now = datetime.now()
current_time = int(now.timestamp())
print(current_time)

程序输出结果

1577761708
1577761708

2. 将时间戳转换为datetime类型数据

from datetime import datetime

current_time = 1577761708
date_time = datetime.fromtimestamp(current_time)
print(date_time)

3. 时间戳与时间元组之间转换

import time

current_time = 1577761708
print(time.ctime(current_time))         # 转换成 Tue Dec 31 11:08:28 2019
print(time.localtime(current_time))     # 转成时间元组,本地时间
print(time.gmtime(current_time))        # 转成时间元组,国际伦敦时间

localtime_tuple = time.localtime(current_time)      # 将时间元组转成时间戳
current_time = time.mktime(localtime_tuple)
print(current_time)

4. 字符串与datetime之间互相转换

from datetime import datetime

now = datetime.now()
now_str = now.strftime("%Y-%m-%d %H:%M:%S")     # 将datetime转成字符串
print(now_str)

date_time = datetime.strptime(now_str, "%Y-%m-%d %H:%M:%S")  # 将字符串转成datetime类型
print(date_time, type(date_time))

5. 字符串与时间元组之间互相转换

import time

time_str = '2019-12-31 11:22:23'
res = time.strptime(time_str, '%Y-%m-%d %H:%M:%S')      # 将字符串转成时间元组
print(res, type(res))

now = time.localtime()
now_str = time.strftime('%Y-%m-%d %H:%M:%S', now)       # 将时间元组转成字符串
print(now_str)

6. datetime 转成时间元组

from datetime import datetime

now = datetime.now()
print(now.ctime())      # 返回ctime() 格式时间字符串
print(now.timetuple())  # 返回时间元组

扫描关注, 与我技术互动

QQ交流群: 211426309

加入知识星球, 每天收获更多精彩内容

分享日常研究的python技术和遇到的问题及解决方案