一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

基于python获取本地时间并转换时间戳和日期格式代码示例

时间:2020-10-27 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下基于python获取本地时间并转换时间戳和日期格式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

附上代码与运行结果截图:

import time

# 获取当前时间
now = time.localtime()
# 格式化日期
now_ = time.strftime('%Y-%m-%d %H:%M:%S', now)
# 获取当前时间,以时间戳格式
now_stamp = time.time()


# 日期转时间戳
change_to_stamp = time.mktime(time.strptime(now_, "%Y-%m-%d %H:%M:%S"))

# 时间戳转日期
change_to_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now_stamp))

print(now)
print(now_)
print(now_stamp)
print(change_to_stamp)
print(change_to_time)

运行截图如下:

热门栏目