python当前日期时间
We can use Python datetime module to get the current date and time of the local system.
我们可以使用Python datetime模块获取本地系统的当前日期和时间。
from datetime import datetime
# Current date time in local system
print(datetime.now())
Output: 2018-09-12 14:17:56.456080
输出: 2018-09-12 14:17:56.456080
Python当前日期 (Python Current Date)
If you are interested only in the date of the local system, you can use the datetime date() method.
如果仅对本地系统的日期感兴趣,则可以使用datetime date()方法。
print(datetime.date(datetime.now()))
Output: 2018-09-12
输出: 2018-09-12
Python当前时间 (Python Current Time)
If you want only time in the local system, use time() method by passing datetime object as an argument.
如果仅在本地系统中需要时间,请通过传递datetime对象作为参数来使用time()方法。
print(datetime.time(datetime.now()))
Output: 14:19:46.423440
输出: 14:19:46.423440
时区中的Python当前日期时间– pytz (Python Current Date Time in timezone – pytz)
Most of the times, we want the date in a specific timezone so that it can be used by others too. Python datetime now() function accepts timezone argument that should be an implementation of tzinfo
abstract base class.
在大多数情况下,我们希望日期位于特定时区,以便其他人也可以使用它。 Python datetime now()函数接受时区参数,该参数应该是tzinfo
抽象基类的实现。
Python pytz is one of the popular module that can be used to get the timezone implementations.
Python pytz是流行的模块之一,可用于获取时区实现。
You can install this module using the following PIP command.
您可以使用以下PIP命令安装此模块。
pip install pytz
Let’s look at some examples of using pytz module to get time in specific timezones.
让我们看一些使用pytz模块获取特定时区时间的示例。
import pytz
utc = pytz.utc
pst = pytz.timezone('America/Los_Angeles')
ist = pytz.timezone('Asia/Calcutta')
print('Current Date Time in UTC =', datetime.now(tz=utc))
print('Current Date Time in PST =', datetime.now(pst))
print('Current Date Time in IST =', datetime.now(ist))
Output:
输出:
Current Date Time in UTC = 2018-09-12 08:57:18.110068+00:00
Current Date Time in PST = 2018-09-12 01:57:18.110106-07:00
Current Date Time in IST = 2018-09-12 14:27:18.110139+05:30
If you want to know all the supported timezone strings, you can print this information using the following command.
如果您想知道所有受支持的时区字符串,则可以使用以下命令打印此信息。
print(pytz.all_timezones)
It will print the list of all the supported timezones by the pytz module.
它将通过pytz模块打印所有受支持的时区的列表。
Python摆锤模块 (Python Pendulum Module)
Python Pendulum module is another timezone library and according to its documentation, it is faster than the pytz module.
Python Pendulum模块是另一个时区库,根据其文档, 它比pytz模块要快 。
We can install Pendulum module using below PIP command.
我们可以使用下面的PIP命令安装Pendulum模块。
pip install pendulum
You can get list of supported timezone strings from pendulum.timezones
attribute.
您可以从pendulum.timezones
属性获取受支持的时区字符串列表。
Let’s look into some examples of getting current date and time information in different time zones using the pendulum module.
让我们看一些使用钟摆模块获取不同时区的当前日期和时间信息的示例。
import pendulum
utc = pendulum.timezone('UTC')
pst = pendulum.timezone('America/Los_Angeles')
ist = pendulum.timezone('Asia/Calcutta')
print('Current Date Time in UTC =', datetime.now(utc))
print('Current Date Time in PST =', datetime.now(pst))
print('Current Date Time in IST =', datetime.now(ist))
Output:
输出:
Current Date Time in UTC = 2018-09-12 09:07:20.267774+00:00
Current Date Time in PST = 2018-09-12 02:07:20.267806-07:00
Current Date Time in IST = 2018-09-12 14:37:20.267858+05:30
翻译自: https://www.journaldev.com/23270/python-current-date-time
python当前日期时间