Date and time is an important part of application development. We generally need to set, get, and change some data time information to our records. In this tutorial, we will examine the python date time now functions and use cases with examples. For more information about Python date-time functions can be get from the following tutorial.
日期和时间是应用程序开发的重要组成部分。 我们通常需要设置,获取和更改一些数据时间信息到我们的记录中。 在本教程中,我们将通过示例检查python date time现在的功能和用例。 有关Python日期时间函数的更多信息,请参见以下教程。
Python Date Time Functions With Examples
带有示例的Python日期时间函数
Python Date Time Operations
Python日期时间操作
In order to use datetime functionalities, we should import the module named datetime . We can import datetime module like below.
为了使用datetime功能,我们应该导入名为datetime的模块。 我们可以像下面那样导入datetime模块。
import datetime
After importing datetime module we can use provided class and functions. The simplest and most basic way to get the current date and time is using datetime.now() function. This resides inside datetime module which means we should use the following code in order to get the current date and time.
导入datetime模块后,我们可以使用提供的类和函数。 获取当前日期和时间的最简单,最基本的方法是使用datetime.now()函数。 它位于datetime模块内部,这意味着我们应该使用以下代码来获取当前日期和时间。
datetime.datetime.now()
Print Now with datetime.now() Function 使用datetime.now()函数立即打印
We can see that every time we execute the now() function it returns different date time information where generally the second changes. The now() function returns date time infomation in datetime format with (YYYY, M, D, H,M,S) format.
我们可以看到,每次执行now()函数时,它都会返回不同的日期时间信息,通常第二个位置发生变化。 现在()函数返回日期时间信息来源在datetime与格式(YYYY, M, D, H,M,S)格式。
While using now() function returned data will be a datetime object. But in some situations, we may need to convert date-time into a string to use string variable or print to the console. We can use str() functions that will convert current date time to string.
使用now()函数时,返回的数据将是日期时间对象。 但是在某些情况下,我们可能需要将日期时间转换为字符串以使用字符串变量或打印到控制台。 我们可以使用str()函数将当前日期时间转换为字符串。
str(datetime.datetime.now())
We can use gmtime() function which will print current GM time. GM time is the current time in Greenwich where the world time starts and ends. We should import time module in order to use gmtime() function.
我们可以使用gmtime()函数来打印当前的GM时间。 GM时间是格林威治时间的开始和结束的当前时间。 为了使用gmtime()函数,我们应该import time模块。
import time
time.gmtime()
Print Now with GM Time 立即打印与通用时间
In this part, we will print an only specific part of the current time. We will use specifiers in order to print only the required part of the time. We assume we have got the current time into a variable named now like below.
在这一部分中,我们将仅打印当前时间的特定部分。 我们将使用说明符,以便仅在部分时间内打印所需的内容。 我们假定我们有当前时间到名为变量now像下面。
now = datetime.datetime.now()
We can print current second like below.
我们可以像下面这样打印当前秒。
now.second
We can print current minute like below.
我们可以像下面那样打印当前分钟。
now.minute
We can print current hour like below.
我们可以像下面那样打印当前时间。
now.hour
We can print current day of month like below.
我们可以像下面那样打印当月的当前日期。
now.day
We can print current month like below.
我们可以像下面那样打印当前月份。
now.month
We can print current year like below.
我们可以像下面那样打印当前年份。
now.year
LEARN MORE Python Script Change Mac Address Periodically
了解更多Python脚本定期更改Mac地址
翻译自: https://www.poftut.com/get-current-date-time-or-now-in-python/