博客
关于我
用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/

你可能感兴趣的文章
Python setuptools sdist:仅安装版本化文件
查看>>
Python Shell下使用matplotlib
查看>>
Python Slice How-to,我知道Python Slice,但我怎么才能使用内置的Slice对象呢?
查看>>
python socket分包发送数据
查看>>
python socket模块_Python socket模块实现TCP服务端客户端
查看>>
Python SOCKS5代理客户端HTTPS
查看>>
Python Soc网络分析:通过使用函数迭代列表来计算机会网络
查看>>
python转换已转义的字符串
查看>>
Python Sphinx自动摘要:成员函数的自动列表
查看>>
Python SQL和NoSQL数据库操作实战
查看>>
python stdout flush_sys.stdout.flush()方法的用法
查看>>
python string 运算
查看>>
Python str与bytes之间的转换
查看>>
Python subprocess ffmpeg
查看>>
python subprocess Permission denied Errno 13
查看>>
Python subprocess.call - 将变量添加到 subprocess.call
查看>>
Python Subprocess.Popen 从一个线程
查看>>
Python subprocess.Popen 作为 Windows 上的不同用户
查看>>
Python转换PPT为PDF
查看>>
Python subprocess.Popen() 等待完成
查看>>