python将时间戳转为天时分秒显示

def seconds_to_str(seconds):
    """
    时间戳转为 天 时 分 秒
    :param seconds: int or float
    :return: str
    eg: 360 --> 6分0秒
    """

    days = int(seconds // (3600 * 24))
    hours = int((seconds // 3600) % 24)
    minutes = int((seconds // 60) % 60)
    seconds = round(seconds % 60)
    if days > 0:
        return f'{days}天{hours}时{minutes}分{seconds}秒'
    if hours > 0:
        return f'{hours}时{minutes}分{seconds}秒'
    if minutes > 0:
        return f'{minutes}分{seconds}秒'
    return f'{seconds}秒'
posted @ 2022-06-06 17:52  cnblogs用户  阅读(697)  评论(0)    收藏  举报