如何在Python中使用Now()函数获取当前日期时间?

本文详细介绍了Python中的datetime模块及其now(), str(), gmtime()等核心函数,包括如何获取当前时间,转换为字符串,以及处理时间的特定部分。通过实例演示,帮助开发者更好地理解和运用这些日期时间功能。
摘要由CSDN通过智能技术生成

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日期时间操作

导入datetime模块以使用datetime函数(Import datetime Module To Use datetime Functions)

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

使用datetime.now()函数立即打印 (Print Now with datetime.now() Function)

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)格式。

现在(当前时间)转换为字符串 (Convert Now (Current Time) To String)

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())

现在根据GM打印(当前时间)(Greenewitch同时进行) (Print Now (Current Time) According To GM (Greenewitch Meantime))

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
Print Now with GM Time
立即打印与通用时间

打印现在(当前时间)的特定部分(Print Specific Part Of Now (Current 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()

打印当前秒 (Print Current Second)

We can print current second like below.

我们可以像下面这样打印当前秒。

now.second

打印当前分钟 (Print Current Minute)

We can print current minute like below.

我们可以像下面那样打印当前分钟。

now.minute

打印当前时间 (Print Current Hour)

We can print current hour like below.

我们可以像下面那样打印当前时间。

now.hour

打印当前月份 (Print Current Day of Month)

We can print current day of month like below.

我们可以像下面那样打印当月的当前日期。

now.day

打印当前月份 (Print Current Month)

We can print current month like below.

我们可以像下面那样打印当前月份。

now.month

打印当前年份 (Print Current Year)

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值