当前位置:首页 > Python > 正文内容

返回当前时间及格式化已使用时间

lonely4年前 (2021-06-24)Python1127
import time
import datetime

# 格式化输出使用时间
class Farmat_time():
    def __init__(self,used_time):
        self.used_time = used_time

    def single_used_time(self):
        used_time1 = datetime.datetime.fromtimestamp(self.used_time,tz=datetime.timezone.utc)
        used_time2 = used_time1.strftime('%M分%S秒')
        return used_time2

    def total_used_time(self):
        used_time1 = datetime.datetime.fromtimestamp(self.used_time,tz=datetime.timezone.utc)
        used_time2 = used_time1.strftime('%H小时%M分%S秒')
        return used_time2

# 返回实时时间
def now_time():
    now_time = time.strftime('%H:%M:%S',time.localtime(time.time()))
    return now_time
    
# 开始时间
start_time = time.time()
time.sleep(5)
# 结束时间
end_time = time.time()
# 得到已使用时间
used_time = end_time - start_time

# 打印当前时间
print('当前时间', now_time())

# 格式化输出使用时间
ftime = Farmat_time(used_time)
print('已使用时间', ftime.single_used_time())


扫描二维码推送至手机访问。

版权声明:本文由复制者发布,如需转载请注明出处。

本文链接:https://www.copyer.cn/post/11.html

分享给朋友:

相关文章

Python pip更换安装源

Python pip更换安装源

常见的镜像源:华为:https://repo.huaweicloud.com/repository/pypi/simple 清华:https://pypi.tuna.tsinghua.edu...

推荐一个好用的Python第三方日志库loguru

推荐一个好用的Python第三方日志库loguru

        Loguru: 更优雅的日志记录解决方案loguru 是一个 Python 简易且强大的第三方日志记录库...

python获取昨天的日期

python获取昨天的日期

import datetime def getYesterday():     today = datetime.d...

windows中配置conda环境变量

windows中配置conda环境变量

一、下载(根据自己的电脑系统下载对应的安装包)官方下载地址:https://www.anaconda.com/products/distribution二、安装下载后双击安装包安装,可以一直下一步默认...

PyQt5安装

PyQt5安装

下面直接使用pip来安装PyQt5,此处可能是pip/pip3,或者两者皆可,后面不再重复直接pip安装PyQt5pip install PyQt5由于Qt Designer已经在...

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment. deactivate and run conda rem...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。