带有示例的Python datetime isoformat()方法

Python datetime.isoformat()方法 (Python datetime.isoformat() Method)

datetime.isoformat() method is used to manipulate objects of datetime class of module datetime.

datetime.isoformat()方法用于操作模块datetime的datetime类的对象。

It uses a datetime class object and returns a string representing the date and time in ISO 8601 format:

它使用datetime类对象,并以ISO 8601格式返回表示日期和时间的字符串:

  • YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0

    YYYY-MM-DDTHH:MM:SS.ffffff (如果微秒不为0)

  • YYYY-MM-DDTHH:MM:SS, if microsecond is 0

    YYYY-MM-DDTHH:MM:SS ,如果微秒为0

If the datetime is an aware object, then a string is added which tells with UTC offset:

如果datetime是可感知的对象,那么将添加一个字符串,该字符串以UTC偏移量表示:

  • YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]], if microsecond is not 0

    如果微秒不为0,则为YYYY-MM-DDTHH:MM:SS.ffffff + HH:MM [:SS [.ffffff]]

  • YYYY-MM-DDTHH:MM:SS+HH:MM[:SS[.ffffff]], if microsecond is 0

    如果微秒为0,则YYYY-MM-DDTHH:MM:SS + HH:MM [:SS [.ffffff]]

The International Standard for the representation of dates and times is ISO 8601. It was designed to provide a format of date and time representation free from any ambiguity.

日期和时间表示的国际标准是ISO8601。该标准旨在提供日期和时间表示的格式,没有任何歧义。

Module:

模块:

    import datetime

Class:

类:

    from datetime import datetime

Syntax:

句法:

    isoformat(sep='T', timespec='auto')

Parameter(s):

参数:

  • sep: is a one-character separator, placed between the date and time portions of the result. The default value is T.

    sep :是一个单字符分隔符,位于结果的日期和时间部分之间。 默认值为T。

  • timespec: is the number of additional components of the time to include. Its default value is auto. It is same as 'seconds' if microsecond is 0, or otherwise, it is microsecond.

    timespec :是要包括的时间中其他部分的数量。 其默认值为auto 。 如果微秒为0,则与“秒”相同,否则为微秒。

Both of these parameters are optional.

这两个参数都是可选的。

Return value:

返回值:

The return type of this method is a string in ISO 8601 format of date and time.

此方法的返回类型是日期和时间的ISO 8601格式的字符串。

Example:

例:

## importing datetime class
from datetime import datetime
import pytz

## Naive object isoformat() example

x = datetime.today()
print("Normal format:",x)
d = x.isoformat()
print("ISO 8601 format:", d)
print("ISO 8601 format without separator 'T':", x.isoformat(sep=' '))
print()

## Microsecond is not 0 here
x = datetime(2020, 10, 1, 12, 12, 12, 3400)
print("ISO 8601 format:", x.isoformat())
print("ISO format without separator 'T':", x.isoformat(sep=' '))
print()

## Microsecond is 0 here
x = datetime(200,10,12,1,1,1)
print("Date 200/10/12 1:1:1 in ISO 8601 format:", x.isoformat())
print()

## Aware object isoformat example

##Microsecond is 0 here
x = datetime(2020,10,4)
timezone = pytz.timezone("Asia/Tokyo")
x = x.astimezone(timezone)##Adding tzinfo
print("ISO 8601 format for an aware object:", x.isoformat())

## Microsecond is not 0 here
x = datetime(2020,10,4,1,1,1,333)
timezone = pytz.timezone("Asia/Tokyo")
x = x.astimezone(timezone)##Adding tzinfo
print("ISO 8601 format for an aware object:", x.isoformat())

Output

输出量

Normal format: 2020-05-01 22:52:37.596841
ISO 8601 format: 2020-05-01T22:52:37.596841
ISO 8601 format without separator 'T': 2020-05-01 22:52:37.596841

ISO 8601 format: 2020-10-01T12:12:12.003400
ISO format without separator 'T': 2020-10-01 12:12:12.003400

Date 200/10/12 1:1:1 in ISO 8601 format: 0200-10-12T01:01:01

ISO 8601 format for an aware object: 2020-10-04T09:00:00+09:00
ISO 8601 format for an aware object: 2020-10-04T10:01:01.000333+09:00


翻译自: https://www.includehelp.com/python/datetime-isoformat-method-with-example.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值