博客
关于我
用python3获取当前系统时间:年月日时分秒。2021-01-25
阅读量:271 次
发布时间:2019-03-01

本文共 725 字,大约阅读时间需要 2 分钟。

在Python中,time模块提供了处理时间的功能,特别是time.time()函数,它返回当前时间的时间戳(从1970年纪元后经过的浮点秒数)。如果你想了解当前系统时间,可以使用time.localtime()函数,它会将时间戳格式化为本地时间。此外,time.strftime()函数可以将时间戳转换为更易读的字符串格式。

以下是time.strftime()中常用的格式化符号及其作用:

  • %Y:四位数的年份(例如:2023)
  • %m:月份(01-12)
  • %d:日(01-31)
  • %H:24小时制的小时数(00-23)
  • %M:分钟(00-59)
  • %S:秒(00-59)
  • %A:完整的星期名称(例如:星期一)
  • %B:完整的月份名称(例如:一月)
  • %I:12小时制的小时数(01-12)

这些格式化符号可以组合使用,来满足不同的日期和时间显示需求。例如,你可以使用“%Y-%m-%d %H:%M:%S”来显示日期和时间。

需要注意的是,time.time()返回的是当前时间的时间戳,而time.localtime()则会将该时间戳转换为本地时间。记住,time.strftime()函数接收time.localtime()或time.time()的输出,因此可以灵活地进行时间格式化。

如果你想实时查看当前时间,可以使用以下代码:

import timeprint('时间显示')for i in range(3):    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))    time.sleep(1)

每次循环都会打印当前系统时间,并在3秒后结束循环。

转载地址:http://tsla.baihongyu.com/

你可能感兴趣的文章
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>