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

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>