Python 时间戳转时间

一、 10 位 时间戳 ,秒级 转换为时间

方式一:

python 复制代码
import time

timeStamp = 1717149862
timeArray = time.localtime(timeStamp)
timeStr = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(timeStr)

'''
输出:
2024-05-31 18:04:22
'''

方式二:

python 复制代码
import datetime

timeStamp = 1717149862
dateArray = datetime.fromtimestamp(timeStamp)
timeStr = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(timeStr)

# 转换为 UTC 时间,相差8小时
timeStamp = 1717149862
dateArray = datetime.utcfromtimestamp(timeStamp)
timeStr = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(timeStr) 


'''
输出:
2024-05-31 18:04:22
2024-05-31 10:04:22
'''

二、 13位时间戳转换为时间字符串

方式一:

python 复制代码
from datetime import datetime

def timestamp_to_strtime(timestamp: int):
    """将 13 位整数的毫秒时间戳转化成本地普通时间 (字符串格式)
    :param timestamp: 13 位整数的毫秒时间戳 (1717149892810)
    :return: 返回字符串格式 {str} '2024-05-31 18:03:41'
    """
    timeArray =  datetime.fromtimestamp(timestamp / 1000.0)
    strtime =timeArray.strftime('%Y-%m-%d %H:%M:%S.%f')  # .%f 带不带都可
    return strtime

timeStr = timestamp_to_strtime(1717149892810)
print(timeStr)


'''
输出:
2024-05-31 18:04:52.810000
'''

方式二:

python 复制代码
import datetime

def timestamp_to_str(timestamp: int, time_format: str = "%Y-%m-%d %H:%M:%S") -> str:
    """
    时间戳转换为时间字符串
    :param timestamp: 时间戳
    :param time_format: 时间字符串格式 default: %Y-%m-%d %H:%M:%S
    :return: 时间字符串
    Usage::
      >>> timestamp_to_str(1717149892810, "%Y-%m-%d")
      2024-05-31
      >>> timestamp_to_str(1717149892810)
      2024-05-31 18:04:52
      >>> timestamp_to_str(1717149892810, "%Y-%m-%d %H:%M:%S.%f")
      2024-05-31 18:04:52.000000

    """
    try:
    	# 13 位 时间戳,毫秒级
        datetime_type = datetime.datetime.fromtimestamp(timestamp // 1000)
        # 10 位 时间戳,秒级
        # datetime_type = datetime.datetime.fromtimestamp(timestamp)
        return datetime_type.strftime(time_format)
    except (TypeError, ValueError):
        raise ValueError("Invalid timestamp format!")

timeStr = timestamp_to_str(1717149892810)
print(timeStr)


'''
输出:
2024-05-31 18:04:52
'''

'''

参考:

时间戳转换

https://tool.lu/timestamp/

Python3日期与时间戳转换的几种方法

https://zhuanlan.zhihu.com/p/67950661

Python时间戳和时间类型及其互相转换

https://www.cnblogs.com/sablier/p/14251436.html

【python】 datetime、13位和10位时间戳、字符串的相互转换

https://blog.csdn.net/weixin_42221654/article/details/131477857

python时间相互转换

https://py-code.readthedocs.io/zh/latest/Python/time_utils/index.html

'''

相关推荐
阿豪只会阿巴9 分钟前
【多喝热水系列】从零开始的ROS2之旅——Day10 话题的订阅与发布1:Python
开发语言·c++·python·ubuntu·ros2
橙露1 小时前
时间序列分析实战:用 Python 实现股票价格预测与风险评估
人工智能·python·机器学习
神云瑟瑟1 小时前
看langchain理解python中的链式调用
python·langchain·链式调用
栈与堆1 小时前
LeetCode 21 - 合并两个有序链表
java·数据结构·python·算法·leetcode·链表·rust
CCPC不拿奖不改名1 小时前
循环神经网络RNN:整数索引→稠密向量(嵌入层 / Embedding)详解
人工智能·python·rnn·深度学习·神经网络·自然语言处理·embedding
鹤入云霄1 小时前
基于Python的空气质量监测系统
python
长行3 小时前
Python|Windows 安装 DeepSpeed 安装方法及报错 Unable to pre-compile async_io 处理
windows·python·deepspeed
百锦再3 小时前
python之路并不一马平川:带你踩坑Pandas
开发语言·python·pandas·pip·requests·tools·mircro
Python之栈3 小时前
5款拖拽式Python GUI生成器助你快速打造炫酷界面
python
灏瀚星空3 小时前
基于 Python 与 GitHub,打造个人专属本地化思维导图工具全流程方案(上)
开发语言·人工智能·经验分享·笔记·python·个人开发·visual studio