python时间模块倒计时

In this tutorial, we will learn about the Python time module. Python Variable is discussed in our previous tutorial. Python time module is helpful when your work needs synchronization with system time.

在本教程中,我们将学习Python时间模块。 我们之前的教程中讨论了Python变量 。 当您的工作需要与系统时间同步时,Python时间模块非常有用。

Python时间模块 (Python time Module)

When you work for a real time project, you may need to synchronize your task with system time. For example, when it’s 8 o’clock, your program should send a message to a group of people. For that purpose, you should know how to get system time using python code.

当您从事实时项目时,您可能需要将任务与系统时间同步。 例如,在8点钟时,您的程序应向一群人发送一条消息。 为此,您应该知道如何使用python代码获取系统时间。

To do so, you need to import Python time module. To get the current time we need to use localtime() function from the time module. The function gets input from time() function. The following code will help you to print current time in python.

为此,您需要导入Python时间模块。 要获取当前时间,我们需要使用时间模块中的localtime()函数。 该函数从time()函数获取输入。 以下代码将帮助您在python中打印当前时间。

# import the module
import time# get the current clock ticks from the time() function
seconds = time.time()currentTime = time.localtime(seconds)# print the currentTime variable to know about it
print(currentTime,'\n')# use current time to show current time in formatted string
print('Current System time is :', time.asctime(currentTime))

And your output will be similar to this

您的输出将与此类似

Python时间格式 (Python time format)

We can use strftime function to format the time.

我们可以使用strftime函数格式化时间。

print('Python Time Formatted is :', time.strftime("%d/%m/%Y", currentTime))

The output produced will be like below.

产生的输出将如下所示。

Python Time Formatted is : 23/08/2017

日历模块 (Calendar Module)

In the previous section, we talked about Python’s time module. We can get current time using python time module and then format it as we require.

在上一节中,我们讨论了Python的时间模块。 我们可以使用python时间模块获取当前时间,然后根据需要设置其格式。

In this section, we will use Python’s calendar module to get information about calendars. The following example code will show us some functions of the calendar module.

在本节中,我们将使用Python的日历模块来获取有关日历的信息。 以下示例代码将向我们展示日历模块的一些功能。

# import the module
import calendar# print the current month
print('The month of August is:\n', calendar.month(2018, 8))# set the first day of the week as sunday
calendar.setfirstweekday(6)# re print the calender
print('The month of August is:\n', calendar.month(2018, 8))# print if a year is leap year
print('Is 2017 a leap year? Ans:', calendar.isleap(2017))
print('Is 2016 a leap year? Ans:', calendar.isleap(2016))

The output of the following code will be

以下代码的输出将是

The month of August is:August 2018
Mo Tu We Th Fr Sa Su1  2  3  4  56  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31The month of August is:August 2018
Su Mo Tu We Th Fr Sa1  2  3  45  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31Is 2017 a leap year? Ans: False
Is 2016 a leap year? Ans: True

There are other modules other than time and calendar. For example, datetime module, dateutil module.

除了时间和日历,还有其他模块。 例如, datetime模块, dateutil模块。

If you get some time, consider reading Pythons Official Reference for more information.

如果有时间,请考虑阅读《 Pythons官方参考》以获取更多信息。

翻译自: https://www.journaldev.com/15193/python-time

python时间模块倒计时

python时间模块倒计时_Python时间模块相关推荐

  1. python常用模块用法_python常用模块(一)

    #什么是模块呢?就是用一大坨代码来完成一个功能的代码集合,是不是简单易懂 #类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个 ...

  2. python常用模块一览表_python 常用模块

    时间模块:time 与 datatime Python中表示时间的方式: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行&qu ...

  3. python模块总结_Python常用模块资料总结和归纳

    Python模块是什么? 一个完整大型的python程序是由模块和包的形式组织起来的,可见模块在python中的重要性.模块是一种组织型式,它许多有关联(关系)的代码组织放到单独的独立文件中.简单的说 ...

  4. python微秒级别等待_python时间操作—获取微秒级别的时间差

    python中有两个模块可以完成时间操作:time和datetime(相比较datetime更强大) 以下分别是两个模块的具体信息: >>> dir(time) ['_STRUCT_ ...

  5. python中的模块原则_python 的模块与包

    一 模块 1.什么是模块 ​ 模块是一系列功能的结合体 ​分为三大类: ​1.内置的模块 ​2.第三方的模块 ​3.自定义的模块 ​一个 python 文件本身就是一个模块,文件名:m.py 模块名: ...

  6. python如何创建模块教程_Python创建模块及模块导入的方法

    本文实例讲述了Python创建模块及模块导入的方法.分享给大家供大家参考.具体分析如下: python学习手册中写道: 定义模块,只要使用文本编辑器,把一些python代码输入到文本中,然后以.py为 ...

  7. python变量和常量_Python数学模块常量和示例

    python变量和常量 Python数学模块常量 (Python math module constants) In the math module, there are some of the de ...

  8. python引入模块教程_python导入模块--案例

    1 导入模块 1.1 问题 本案例要求先编写一个star模块,主要要求如下: 建立工作目录 ~/bin/ 创建模块文件 ~/bin/star.py 模块中创建pstar函数,实现打印50个星号的功能 ...

  9. python 逐行读取文件_Python fileinput模块:逐行读取多个文件

    前面章节中,我们学会了使用 open() 和 read()(或者 readline().readlines() )组合,来读取单个文件中的数据.但在某些场景中,可能需要读取多个文件的数据,这种情况下, ...

最新文章

  1. HTML DOM教程 22-HTML DOM Form 对象
  2. Xcodebuild自动打包
  3. mysql5.7.16安装版_mysql数据库5.7.16安装版怎么安装图解
  4. 雷军宣布红米 Redmi 品牌独立,这对小米意味着什么?
  5. java给腾讯通发消息_通过web 向rtx发送通知消息
  6. Nginx 的线程池与性能剖析【转载】
  7. 超越软件上云 讲述每天1亿公里行驶背后的大生意
  8. S5PV210体系结构与接口02:ARM编程模型 汇编指令
  9. mplab 语法错误不报错_Python怎么解决报错?
  10. 绕过安全狗_某教程学习笔记(一):23、WAF绕过
  11. Mac上配置Privoxy
  12. Linux内核线程(一)
  13. JAVA--set用法
  14. cavium公司和xPliant芯片
  15. 科创板第二批受理名单公布,为何AI独角兽 “全军覆没”?...
  16. DB---数据模型的三要素
  17. 云服务器基本信息的公网ip在哪,如何查看云服务器的公网ip地址
  18. sgu244:Height, Bisector and Median(几何)
  19. 华为nova6计算机历史在哪可以看,写在华为nova6发布前:一文看出nova手机使用芯片的变迁历程...
  20. win10输入法频繁自动中英文切换的问题

热门文章

  1. C# File流操作
  2. php单字母函数(快捷方法)使用总结转载
  3. 看懂别人的代码,和自己能写代码是两回事
  4. 用gallery展示图片,实现中间图片稍大,两边较小的效果
  5. IE6 的 hover 伪类 bug
  6. Linux性能监控(转)
  7. [转载] python difference用法_set.difference() 的用法(python3)_举例说明python3 set方法功能
  8. [转载] 快速入门(完整):Python实例100个(基于最新Python3.7版本)
  9. Java匹马行天下之C国程序员的秃头原因
  10. 【Codeforces 486C】Palindrome Transformation