python当前日期时间_Python当前日期时间

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
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23270/python-current-date-time

python当前日期时间

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值