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

你可能感兴趣的文章
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>
npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
查看>>
npm—小记
查看>>