python日期时间
Python日期时间 (Python datetime)
In this post, we will study about how to use the python datetime module. With datetime module, we can get information about datetime and format dates as well.
在本文中,我们将研究如何使用python datetime 模块 。 使用datetime模块,我们还可以获得有关日期时间和格式日期的信息。
使用日期时间模块 (Using datetime module)
Let’s start working on python datetime module straightaway.
让我们直接开始使用python datetime模块。
python datetime now() (Python datetime now())
It is easy to print current time using datetime
module. Let’s see the code snippet on how this can be done:
使用datetime
模块可以轻松打印当前时间。 让我们看一下如何做到这一点的代码片段:
import time
import datetime
print("Time in seconds since the epoch: %s", time.time())
print("Current date and time: " , datetime.datetime.now())
Let’s see the output for this program:
让我们看一下该程序的输出:
At first, accessing a property inside datetime module which has the same name looks odd. You can call the now()
function to print the date information, as shown above. Notice that datetime now() returns the date-time information in a user-friendly way and looks much better than we get from time module.
首先,访问具有相同名称的datetime模块内部的属性看起来很奇怪。 您可以调用now()
函数以打印日期信息,如上所示。 注意datetime now()以一种用户友好的方式返回日期时间信息,并且看起来比我们从time模块获得的要好得多。
Python日期时间格式 (Python datetime format)
Most of the times, single date format do not fulfill our needs in the application. We can format our code using the code snippet we show next:
在大多数情况下,单日期格式不能满足我们在应用程序中的需求。 我们可以使用下面显示的代码片段来格式化代码:
import datetime
print("Formatted date : " , datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))
Let’s see the output for this program:
Note that here, the smaller
m
reflects the Month and capital
M
shows the minute part for time. This often leads to confusions and is a point to remember.
让我们看一下该程序的输出:
请注意,此处较小的m
表示月份,而大写M
表示时间的分钟部分。 这通常会导致混乱,并且是要记住的一点。
Python日期时间示例 (Python datetime example)
We can do a lot more with datetime format like getting the present year, calculating weekday of the day, day of the year, etc. All of this is possible with single calls and we just need to use the strftime()
function appropriately. Let’s see the code snippet on how this can be done:
我们可以使用日期时间格式做更多的事情,例如获取当前年份,计算一天中的工作日,一年中的某天等等。所有这些都可以通过一次调用来实现,而我们只需要适当地使用strftime()
函数即可。 让我们看一下如何做到这一点的代码片段:
import datetime
print("Present year: ", datetime.date.today().strftime("%Y"))
print("Month of year: ", datetime.date.today().strftime("%B"))
print("Week number of the year: ", datetime.date.today().strftime("%W"))
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
print("Day of year: ", datetime.date.today().strftime("%j"))
print("Day of the month : ", datetime.date.today().strftime("%d"))
print("Day of week: ", datetime.date.today().strftime("%A"))
Let’s see the output for this program:
Now that was a lot of information which can be used.
让我们看一下该程序的输出:
现在,有很多信息可以使用。
获取工作日的日期 (Getting Weekday for a Date)
It is also possible to get a weekday for a given date. This is sometimes needed in calendar-based applications or just for curiosity!
也可以在给定日期获得工作日。 在基于日历的应用程序中有时出于好奇心而需要这样做!
Let’s see the code snippet on how this can be done:
让我们看一下如何做到这一点的代码片段:
import datetime
birthday = datetime.date(1994,5, 20) #year, month, day
print(birthday.strftime("%A"))
Let’s see the output for this program:
让我们看一下该程序的输出:
Python字符串到日期时间 (Python String to datetime)
We can use strptime()
function to get datetime object from String. Let’s see a short example for this.
我们可以使用strptime()
函数从String获取datetime对象。 让我们看一个简短的例子。
import datetime
time_now = datetime.datetime.strptime("1/1/2018", "%m/%d/%Y")
print(time_now)
print(type(time_now))
The output of the above program is shown below.
上面程序的输出如下所示。
2018-01-01 00:00:00
<class 'datetime.datetime'>
结论 (Conclusion)
In this post, we saw how we can use the Python datetime module to provide date information and put it to use in our application. It’s a very useful module to provide date and time information in a user-friendly way. Also, there are many functions for formatting and getting specific information. So this module comes very handily in python application dealing with dates.
在本文中,我们看到了如何使用Python datetime模块提供日期信息并将其用于我们的应用程序。 这是一个非常有用的模块,以用户友好的方式提供日期和时间信息。 此外,还有许多用于格式化和获取特定信息的功能。 因此,此模块在处理日期的python应用程序中非常方便。
Reference: API Doc
参考: API文档
python日期时间