博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python当前日期时间_Python当前日期时间
阅读量:2540 次
发布时间:2019-05-11

本文共 3296 字,大约阅读时间需要 10 分钟。

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 systemprint(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 is one of the popular module that can be used to get the timezone implementations.

Python 是流行的模块之一,可用于获取时区实现。

You can install this module using the following command.

您可以使用以下命令安装此模块。

pip install pytz

Let’s look at some examples of using pytz module to get time in specific timezones.

让我们看一些使用pytz模块获取特定时区时间的示例。

import pytzutc = pytz.utcpst = 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:00Current Date Time in PST = 2018-09-12 01:57:18.110106-07:00Current 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, .

Python Pendulum模块是另一个时区库,根据其文档, 。

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 pendulumutc = 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:00Current Date Time in PST = 2018-09-12 02:07:20.267806-07:00Current Date Time in IST = 2018-09-12 14:37:20.267858+05:30
. 检出完整的python脚本和更多Python示例。

翻译自:

python当前日期时间

转载地址:http://lxmzd.baihongyu.com/

你可能感兴趣的文章
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>