Tạo ngày hàng tuần python

Mặc dù số học ngày và giờ được hỗ trợ, trọng tâm của việc triển khai là trích xuất thuộc tính hiệu quả để định dạng và thao tác đầu ra

Xem thêm

Module

General calendar related functions

Module

Time access and conversions

Module

Concrete time zones representing the IANA time zone database

Package dateutil

Third-party library with expanded time zone and parsing support

Aware and Naive Objects

Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information

With sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation.

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality

For applications requiring aware objects, and objects have an optional time zone information attribute,

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
4, that can be set to an instance of a subclass of the abstract class. These objects capture information about the offset from UTC time, the time zone name, and whether daylight saving time is in effect

Only one concrete class, the class, is supplied by the module. The class can represent simple timezones with fixed offsets from UTC, such as UTC itself or North American EST and EDT timezones. Supporting timezones at deeper levels of detail is up to the application. The rules for time adjustment across the world are more political than rational, change frequently, and there is no standard suitable for every application aside from UTC

Constants

The module exports the following constants

datetime. MINYEAR

The smallest year number allowed in a or object. is

>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
5

datetime. MAXYEAR

The largest year number allowed in a or object. is

>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
9

datetime. UTC

Alias for the UTC timezone singleton

New in version 3. 11

Available Types

class datetime. date

An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes. , , and

class datetime. time

An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds. (There is no notion of “leap seconds” here. ) Attributes. , , , , và

class datetime. datetime

A combination of a date and a time. Attributes. , , , , , , , and

class datetime. timedelta

Khoảng thời gian thể hiện sự khác biệt giữa hai , hoặc phiên bản đối với độ phân giải micro giây

lớp ngày giờ. tzinfo

Một lớp cơ sở trừu tượng cho các đối tượng thông tin múi giờ. Chúng được sử dụng bởi các lớp và để cung cấp khái niệm điều chỉnh thời gian có thể tùy chỉnh (ví dụ: để tính múi giờ và/hoặc thời gian tiết kiệm ánh sáng ban ngày)

lớp ngày giờ. múi giờ

Một lớp triển khai lớp cơ sở trừu tượng dưới dạng phần bù cố định từ UTC

Mới trong phiên bản 3. 2

Các đối tượng của các loại này là bất biến

quan hệ phân lớp

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime

Tài sản chung

, , và các loại chia sẻ các tính năng phổ biến này

  • Các đối tượng của các loại này là bất biến

  • Các đối tượng thuộc loại này có thể băm được, nghĩa là chúng có thể được sử dụng làm khóa từ điển

  • Các đối tượng thuộc loại này hỗ trợ tẩy hiệu quả thông qua mô-đun

Determining if an Object is Aware or Naive

Objects of the type are always naive

An object of type or may be aware or naive

A object d is aware if both of the following hold

  1. >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    02 is not
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

  2. >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    04 does not return
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

Otherwise, d is naive

A object t is aware if both of the following hold

  1. >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    07 is not
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

  2. >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    09 does not return
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

Otherwise, t is naive

Sự khác biệt giữa nhận thức và ngây thơ không áp dụng cho các đối tượng

Objects

A object represents a duration, the difference between two dates or times

class datetime. timedelta(days=0 , seconds=0 , microseconds=0 , milliseconds=0 , minutes=0 , hours=0 , weeks=0)

All arguments are optional and default to

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
14. Arguments may be integers or floats, and may be positive or negative

Only days, seconds and microseconds are stored internally. Arguments are converted to those units

  • A millisecond is converted to 1000 microseconds

  • A minute is converted to 60 seconds

  • An hour is converted to 3600 seconds

  • A week is converted to 7 days

and days, seconds and microseconds are then normalized so that the representation is unique, with

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    15

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    16 (the number of seconds in one day)

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    17

The following example illustrates how any arguments besides days, seconds and microseconds are “merged” and normalized into those three resulting attributes

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)

If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond using round-half-to-even tiebreaker. If no argument is a float, the conversion and normalization processes are exact (no information is lost)

If the normalized value of days lies outside the indicated range, is raised

Note that normalization of negative values may be surprising at first. Ví dụ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

Class attributes

timedelta. min

The most negative object,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
20

timedelta. max

The most positive object,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
22

timedelta. resolution

The smallest possible difference between non-equal objects,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
24

Note that, because of normalization,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
25 >
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
26.
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
27 is not representable as a object

Instance attributes (read-only)

Attribute

Value

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
29

Between -999999999 and 999999999 inclusive

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
30

Between 0 and 86399 inclusive

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
31

Between 0 and 999999 inclusive

Supported operations

Operation

Result

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
32

Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. (1)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
33

Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. (1)(6)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
34

Delta multiplied by an integer. Afterwards t1 // i == t2 is true, provided

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
35

In general, t1 * i == t1 * (i-1) + t1 is true. (1)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
36

Delta nhân với một số float. Kết quả được làm tròn đến bội số gần nhất của timedelta. độ phân giải sử dụng nửa vòng đến chẵn

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
37

Chia (3) tổng thời lượng t2 cho đơn vị khoảng thời gian t3. Trả về một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
39

Delta chia cho float hoặc int. Kết quả được làm tròn đến bội số gần nhất của timedelta. độ phân giải sử dụng nửa vòng đến chẵn

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
40 hoặc
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
41

Sàn được tính và phần còn lại (nếu có) bị vứt đi. Trong trường hợp thứ hai, một số nguyên được trả về. (3)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
42

Phần còn lại được tính như một đối tượng. (3)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
44

Tính thương và số dư.

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
45 (3) và
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
46. q là một số nguyên và r là một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
48

Trả về một đối tượng có cùng giá trị. (2)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
50

tương đương với (-t1. ngày, -t1. giây, -t1. micro giây) và đến t1* -1. (1)(4)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
52

tương đương với +t khi

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
53, và với -t khi
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
54. (2)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
55

Trả về một chuỗi ở dạng

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
56, trong đó D là số âm cho số âm của
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
57. (5)

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
58

Trả về một biểu diễn chuỗi của đối tượng dưới dạng lệnh gọi hàm tạo với các giá trị thuộc tính chuẩn

ghi chú

  1. Điều này là chính xác nhưng có thể tràn

  2. Điều này là chính xác và không thể tràn

  3. Chia cho 0 tăng

  4. -timedelta. max không thể biểu diễn dưới dạng đối tượng

  5. Biểu diễn chuỗi của các đối tượng được chuẩn hóa tương tự như biểu diễn bên trong của chúng. Điều này dẫn đến kết quả hơi bất thường đối với timedelta âm. Ví dụ

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    

  6. Biểu thức

    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    63 sẽ luôn bằng biểu thức
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    64 trừ khi t3 bằng với
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    25;

Ngoài các hoạt động được liệt kê ở trên, các đối tượng hỗ trợ một số phép cộng và phép trừ với và đối tượng (xem bên dưới)

Changed in version 3. 2. Phép chia tầng và phép chia thực sự của một đối tượng cho một đối tượng khác hiện đã được hỗ trợ, cũng như các phép toán còn lại và hàm. Phép chia thực sự và phép nhân của một đối tượng với một đối tượng hiện được hỗ trợ.

So sánh các đối tượng được hỗ trợ, với một số lưu ý

Các phép so sánh

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
75 hoặc
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
76 luôn trả về a , bất kể loại đối tượng được so sánh

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False

Đối với tất cả các phép so sánh khác (chẳng hạn như

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
78 và
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
79), khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

Trong ngữ cảnh Boolean, một đối tượng được coi là đúng khi và chỉ khi nó không bằng

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
83

Phương thức sơ thẩm

đồng bằng thời gian. total_seconds()

Trả về tổng số giây có trong khoảng thời gian. Tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
84. Đối với các đơn vị khoảng thời gian không phải là giây, hãy sử dụng biểu mẫu chia trực tiếp (e. g.
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
85)

Lưu ý rằng trong khoảng thời gian rất lớn (hơn 270 năm trên hầu hết các nền tảng), phương pháp này sẽ mất độ chính xác micro giây

Mới trong phiên bản 3. 2

Ví dụ về cách sử dụng.

Một ví dụ bổ sung về chuẩn hóa

>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0

Ví dụ về số học

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)

Objects

Một đối tượng đại diện cho một ngày (năm, tháng và ngày) trong lịch lý tưởng hóa, lịch Gregorian hiện tại được mở rộng vô thời hạn theo cả hai hướng

Ngày 1 tháng 1 của năm 1 được gọi là ngày 1, ngày 2 tháng 1 của năm 1 được gọi là ngày 2, v.v.

lớp ngày giờ. ngày(năm , tháng, day)

Tất cả các đối số là bắt buộc. Các đối số phải là số nguyên, trong các phạm vi sau

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    90

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    91

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    92

Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên

Các hàm tạo khác, tất cả các phương thức của lớp

phương thức lớp ngày. hôm nay()

Trả về ngày địa phương hiện tại

Điều này tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
94

phương thức lớp ngày. dấu thời gian từ(dấu thời gian)

Trả về ngày địa phương tương ứng với dấu thời gian POSIX, chẳng hạn như được trả về bởi

Điều này có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 của nền tảng và khi
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 thất bại. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038. Lưu ý rằng trên các hệ thống không phải POSIX bao gồm các giây nhuận trong khái niệm dấu thời gian, các giây nhuận bị bỏ qua bởi

Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm C

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 của nền tảng. Tăng thay vì trên
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 thất bại.

phương thức lớp ngày. từ thứ tự(thứ tự)

Trả về ngày tương ứng với thứ tự Gregorian proleptic, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1

được nâng lên trừ khi

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
08. Đối với bất kỳ ngày d,
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
09

phương thức lớp ngày. fromisoformat(date_string)

Trả về một tương ứng với một chuỗi_ngày được cung cấp ở bất kỳ định dạng ISO 8601 hợp lệ nào, ngoại trừ các ngày thứ tự (e. g.

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
11)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)

Mới trong phiên bản 3. 7

Đã thay đổi trong phiên bản 3. 11. Trước đây, phương thức này chỉ hỗ trợ định dạng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
12.

phương thức lớp ngày. từ dương lịch(năm , tuần, day)

Trả về một tương ứng với ngày theo lịch ISO được chỉ định theo năm, tuần và ngày. Đây là nghịch đảo của hàm

Mới trong phiên bản 3. 8

Class attributes

ngày. phút

Ngày đại diện sớm nhất,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
15

ngày. tối đa

Ngày đại diện mới nhất,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
16

ngày. độ phân giải

Sự khác biệt nhỏ nhất có thể giữa các đối tượng ngày không bằng nhau,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
17

Instance attributes (read-only)

ngày. năm

Giữa và bao gồm

ngày. month

Từ 1 đến 12 bao gồm

ngày. ngày

Giữa 1 và số ngày trong tháng nhất định của năm nhất định

Supported operations

Operation

Result

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
20

ngày2 sẽ là

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
21 ngày sau ngày1. (1)

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
22

Tính date2 sao cho

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
23. (2)

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
24

(3)

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
25

date1 được coi là nhỏ hơn date2 khi date1 trước date2 về thời gian. (4)

ghi chú

  1. date2 được di chuyển về phía trước theo thời gian nếu

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    26 hoặc lùi lại nếu
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    27. Sau đó
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    28.
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    29 và
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    30 bị bỏ qua. được nâng lên nếu
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    32 sẽ nhỏ hơn hoặc lớn hơn

  2. >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    29 và
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    30 bị bỏ qua

  3. Điều này là chính xác và không thể tràn. múi giờ. giây và timedelta. micro giây là 0 và date2 + timedelta == date1 sau

  4. Nói cách khác,

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    25 khi và chỉ khi
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    38. So sánh ngày tăng lên nếu so sánh khác cũng không phải là một đối tượng. Tuy nhiên, thay vào đó,
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    41 được trả về nếu đối tượng so sánh khác có thuộc tính
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    42. Móc này cung cấp cho các loại đối tượng ngày khác cơ hội thực hiện so sánh kiểu hỗn hợp. Nếu không, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi so sánh là
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    75 hoặc
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    76. Các trường hợp sau trả về hoặc , tương ứng

Trong ngữ cảnh Boolean, tất cả các đối tượng được coi là đúng

Phương thức sơ thẩm

ngày. thay thế(năm=chính mình. năm , tháng=mình. tháng , ngày=chính mình. ngày)

Trả về một ngày có cùng giá trị, ngoại trừ các tham số được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định

Ví dụ

>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)

ngày. thời gian()

Trả lại một chẳng hạn như được trả lại bởi

Giờ, phút và giây là 0 và cờ DST là -1

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
52 tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
0

trong đó

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
53 là số ngày trong năm hiện tại bắt đầu bằng
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
5 cho ngày 1 tháng 1

ngày. bình thường()

Trả về thứ tự Gregorian proleptic của ngày, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1. Đối với bất kỳ đối tượng d,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
09

ngày. ngày trong tuần()

Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 0 và Chủ Nhật là 6. Ví dụ,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
57, Thứ Tư. Xem thêm

ngày. ngày trong tuần()

Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 1 và Chủ Nhật là 7. Ví dụ,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
59, Thứ Tư. Xem thêm ,

ngày. isocalendar()

Trả về một đối tượng có ba thành phần.

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
1,
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
63 và
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
64

Lịch ISO là một biến thể được sử dụng rộng rãi của lịch Gregorian.

Năm ISO bao gồm 52 hoặc 53 tuần đầy đủ và trong đó một tuần bắt đầu vào Thứ Hai và kết thúc vào Chủ nhật. Tuần đầu tiên của một năm ISO là tuần dương lịch (Gregorian) đầu tiên của một năm có ngày thứ Năm. Đây được gọi là tuần số 1 và năm ISO của ngày thứ Năm đó giống với năm Gregorian của nó

Ví dụ: năm 2004 bắt đầu vào Thứ Năm, vì vậy tuần đầu tiên của năm ISO 2004 bắt đầu vào Thứ Hai, ngày 29 tháng 12 năm 2003 và kết thúc vào Chủ Nhật, ngày 4 tháng 1 năm 2004

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
1

Đã thay đổi trong phiên bản 3. 9. Kết quả đã thay đổi từ một bộ thành một.

ngày. isoformat()

Trả về một chuỗi đại diện cho ngày ở định dạng ISO 8601,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
12

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2

ngày. __str__()

Đối với một ngày d,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
66 tương đương với
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
67

ngày. thời gian()

Trả về một chuỗi đại diện cho ngày

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
3

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
68 tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4

trên các nền tảng có chức năng C

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
69 gốc (gọi, nhưng không gọi) tuân theo tiêu chuẩn C

ngày. thời gian chạy(định dạng)

Trả về một chuỗi đại diện cho ngày, được kiểm soát bởi một chuỗi định dạng rõ ràng. Mã định dạng đề cập đến giờ, phút hoặc giây sẽ thấy 0 giá trị. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

ngày. __format__(định dạng)

Giống như. Điều này giúp có thể chỉ định một chuỗi định dạng cho một đối tượng trong và khi sử dụng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

Ví dụ về cách sử dụng.

Ví dụ về đếm số ngày đến một sự kiện

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
5

Thêm ví dụ về làm việc với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
6

Objects

Một đối tượng là một đối tượng duy nhất chứa tất cả thông tin từ một đối tượng và một đối tượng

Giống như một đối tượng, giả sử lịch Gregorian hiện tại được mở rộng theo cả hai hướng;

Hàm tạo

lớp ngày giờ. ngày giờ(năm , tháng, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

Các đối số năm, tháng và ngày là bắt buộc. tzinfo có thể là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một thể hiện của lớp con. Các đối số còn lại phải là số nguyên trong các phạm vi sau

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    90,

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    91,

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    92,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    90,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    91,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    92,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    93,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    94

Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên

Mới trong phiên bản 3. 6. Đã thêm đối số

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
96.

Các hàm tạo khác, tất cả các phương thức của lớp

phương thức lớp ngày giờ. hôm nay()

Trả về ngày giờ địa phương hiện tại, với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03

Tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
7

Xem thêm ,

Phương thức này có chức năng tương đương với , nhưng không có tham số

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
02

phương thức lớp ngày giờ. bây giờ(tz=Không)

Trả về ngày và giờ địa phương hiện tại

Nếu đối số tùy chọn tz là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc không được chỉ định, thì điều này giống như , nhưng, nếu có thể, cung cấp độ chính xác cao hơn mức có thể nhận được từ việc xem xét dấu thời gian (ví dụ: điều này có thể thực hiện được trên các nền tảng cung cấp hàm C
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
06)

Nếu tz không phải là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, thì nó phải là một thể hiện của một lớp con và ngày giờ hiện tại được chuyển đổi thành múi giờ của tz

Chức năng này được ưa thích hơn và

phương thức lớp ngày giờ. utcnow()

Trả về ngày và giờ UTC hiện tại, với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03

Điều này giống như, nhưng trả về ngày và giờ UTC hiện tại, dưới dạng một đối tượng ngây thơ. Có thể nhận được thời gian UTC hiện tại đã biết bằng cách gọi

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
15. Xem thêm

Cảnh báo

Bởi vì các đối tượng

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 ngây thơ được nhiều phương thức
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC. Như vậy, cách được đề xuất để tạo một đối tượng biểu thị thời gian hiện tại theo UTC là gọi số
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
15

phương thức lớp ngày giờ. dấu thời gian từ(dấu thời gian , tz=None)

Trả về ngày và giờ cục bộ tương ứng với dấu thời gian POSIX, chẳng hạn như được trả về bởi. Nếu đối số tùy chọn tz là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc không được chỉ định, thì dấu thời gian được chuyển đổi thành ngày giờ địa phương của nền tảng và đối tượng được trả về là ngây thơ

Nếu tz không phải là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, nó phải là một thể hiện của một lớp con và dấu thời gian được chuyển đổi thành múi giờ của tz

có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi các hàm C

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 hoặc
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 của nền tảng và trên
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 hoặc
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 không thành công. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038. Lưu ý rằng trên các hệ thống không phải POSIX bao gồm giây nhuận trong khái niệm dấu thời gian, giây nhuận bị bỏ qua và sau đó có thể có hai dấu thời gian khác nhau một giây tạo ra các đối tượng giống hệt nhau. Phương pháp này được ưa chuộng hơn

Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi các hàm C

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 hoặc ________49____28 của nền tảng. Tăng thay vì khi thất bại
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
97 hoặc
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28.

Đã thay đổi trong phiên bản 3. 6. may return instances with set to 1.

phương thức lớp ngày giờ. utcfromtimestamp(dấu thời gian)

Trả lại UTC tương ứng với dấu thời gian POSIX, với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03. (Đối tượng kết quả là ngây thơ. )

Điều này có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 của nền tảng và khi
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 thất bại. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038

Để có được một đối tượng nhận thức, hãy gọi

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8

Trên nền tảng tuân thủ POSIX, nó tương đương với biểu thức sau

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
9

ngoại trừ công thức sau luôn hỗ trợ phạm vi năm đầy đủ. giữa và bao gồm

Cảnh báo

Bởi vì các đối tượng

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 ngây thơ được nhiều phương thức
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC. Như vậy, cách được đề xuất để tạo một đối tượng đại diện cho một dấu thời gian cụ thể trong UTC là gọi số
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
58

Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm C

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 của nền tảng. Tăng thay vì trên
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 thất bại.

phương thức lớp ngày giờ. từ thứ tự(thứ tự)

Trả về tương ứng với thứ tự Gregorian proleptic, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1. được nâng lên trừ khi

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
67. Giờ, phút, giây và micro giây của kết quả đều bằng 0 và là
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03

phương thức lớp ngày giờ. kết hợp(ngày , thời gian . tzinfo, tzinfo=self.tzinfo)

Trả về một đối tượng mới có các thành phần ngày bằng với đối tượng đã cho và có thành phần thời gian bằng với đối tượng đã cho. Nếu đối số tzinfo được cung cấp, giá trị của nó được sử dụng để đặt thuộc tính của kết quả, nếu không thì thuộc tính của đối số thời gian được sử dụng

Đối với bất kỳ đối tượng d,

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
76. Nếu ngày là một đối tượng, các thành phần và thuộc tính thời gian của nó sẽ bị bỏ qua

Đã thay đổi trong phiên bản 3. 6. Đã thêm đối số tzinfo.

phương thức lớp ngày giờ. fromisoformat(date_string)

Trả về một tương ứng với một chuỗi ngày ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau

  1. Độ lệch múi giờ có thể có giây phân số

  2. Dấu phân cách

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    80 có thể được thay thế bằng bất kỳ ký tự unicode nào

  3. Ngày thứ tự hiện không được hỗ trợ

  4. Giờ và phút phân số không được hỗ trợ

ví dụ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
0

Mới trong phiên bản 3. 7

Đã thay đổi trong phiên bản 3. 11. Trước đây, phương pháp này chỉ hỗ trợ các định dạng có thể được phát ra bởi hoặc.

phương thức lớp ngày giờ. từ dương lịch(năm , tuần, day)

Trả về một tương ứng với ngày theo lịch ISO được chỉ định theo năm, tuần và ngày. Các thành phần không phải ngày tháng của ngày giờ được điền bằng các giá trị mặc định bình thường của chúng. Đây là nghịch đảo của hàm

Mới trong phiên bản 3. 8

phương thức lớp ngày giờ. strptime(date_string , định dạng)

Trả về tương ứng với date_string, được phân tích cú pháp theo định dạng

Điều này tương đương với

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
1

được nâng lên nếu không thể phân tích cú pháp chuỗi ngày và định dạng hoặc nếu nó trả về một giá trị không phải là bộ thời gian. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

Class attributes

ngày giờ. phút

Đại diện sớm nhất ,

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
89

ngày giờ. tối đa

Đại diện mới nhất ,

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
91

ngày giờ. độ phân giải

The smallest possible difference between non-equal objects,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
24

Instance attributes (read-only)

ngày giờ. năm

Giữa và bao gồm

ngày giờ. tháng

Từ 1 đến 12 bao gồm

ngày giờ. ngày

Giữa 1 và số ngày trong tháng nhất định của năm nhất định

ngày giờ. giờ

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
96

ngày giờ. phút

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
97

datetime. giây

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
97

ngày giờ. micro giây

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
99

ngày giờ. tzinfo

Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 nếu không có đối số nào được truyền

ngày giờ. gấp

Trong

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. (Một khoảng thời gian lặp lại xảy ra khi đồng hồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ) Giá trị 0 (1) thể hiện sớm hơn (muộn hơn) của hai thời điểm có cùng biểu diễn thời gian tường

Mới trong phiên bản 3. 6

Supported operations

Operation

Result

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
03

(1)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
04

(2)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
05

(3)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
06

So sánh với. (4)

  1. datetime2 là khoảng thời gian của timedelta bị xóa khỏi datetime1, tiến về phía trước nếu

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    21 > 0 hoặc lùi lại nếu
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    21 < 0. Kết quả có cùng thuộc tính với datetime đầu vào và datetime2 - datetime1 == timedelta sau. được nâng lên nếu datetime2. năm sẽ nhỏ hơn hoặc lớn hơn. Lưu ý rằng không có điều chỉnh múi giờ nào được thực hiện ngay cả khi đầu vào là một đối tượng nhận biết

  2. Tính datetime2 sao cho datetime2 + timedelta == datetime1. Ngoài ra, kết quả có cùng thuộc tính với ngày giờ đầu vào và không có điều chỉnh múi giờ nào được thực hiện ngay cả khi đầu vào biết

  3. Phép trừ a từ a chỉ được xác định nếu cả hai toán hạng đều ngây thơ hoặc nếu cả hai đều biết. Nếu một người nhận thức được và người kia ngây thơ, được nuôi dưỡng

    Nếu cả hai đều ngây thơ hoặc cả hai đều nhận thức được và có cùng một thuộc tính, các thuộc tính sẽ bị bỏ qua và kết quả là một đối tượng t sao cho

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    22. Không có điều chỉnh múi giờ nào được thực hiện trong trường hợp này

    Nếu cả hai đều nhận biết và có các thuộc tính khác nhau, thì ________ 55 ______ 24 hoạt động như thể a và b lần đầu tiên được chuyển đổi thành thời gian biểu UTC ngây thơ trước. Kết quả là

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    25 ngoại trừ việc triển khai không bao giờ bị tràn

  4. datetime1 được coi là nhỏ hơn datetime2 khi datetime1 trước datetime2 về thời gian

    Nếu một so sánh là ngây thơ và người kia nhận thức được, sẽ được nâng lên nếu cố gắng so sánh thứ tự. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng các trường hợp nhận thức

    Nếu cả hai đối tượng so sánh đều nhận biết và có cùng thuộc tính, thì thuộc tính chung sẽ bị bỏ qua và thời gian dữ liệu cơ sở được so sánh. Nếu cả hai bộ so sánh đều nhận biết và có các thuộc tính khác nhau, thì các bộ so sánh trước tiên được điều chỉnh bằng cách trừ đi độ lệch UTC của chúng (thu được từ

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    30)

    Đã thay đổi trong phiên bản 3. 3. So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ không nâng cao.

    Ghi chú

    Để ngăn việc so sánh quay trở lại sơ đồ so sánh địa chỉ đối tượng mặc định, so sánh ngày giờ thường tăng lên nếu đối tượng so sánh khác cũng không phải là một đối tượng. Tuy nhiên, thay vào đó,

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    41 được trả về nếu đối tượng so sánh khác có thuộc tính
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    42. Móc này cung cấp cho các loại đối tượng ngày khác cơ hội thực hiện so sánh kiểu hỗn hợp. Nếu không, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi so sánh là
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    75 hoặc
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    76. Các trường hợp sau trả về hoặc , tương ứng

Phương thức sơ thẩm

ngày giờ. ngày()

Trả về đối tượng có cùng năm, tháng và ngày

ngày giờ. thời gian()

Trả về đối tượng có cùng giờ, phút, giây, micro giây và lần. là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03. Xem thêm phương pháp

Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về.

ngày giờ. timetz()

Trả về đối tượng có cùng thuộc tính giờ, phút, giây, micro giây, nếp gấp và tzinfo. Xem thêm phương pháp

Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về.

ngày giờ. thay thế(năm=chính mình. năm , tháng=mình. tháng , ngày=chính mình. ngày , giờ=chính mình. giờ , phút=chính mình. phút , giây=chính mình. giây , micro giây=chính mình. micro giây , tzinfo=self. tzinfo , * , gấp=0)

Trả về một ngày giờ có cùng thuộc tính, ngoại trừ những thuộc tính được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng có thể chỉ định

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
52 để tạo một ngày giờ ngây thơ từ một ngày giờ nhận thức mà không cần chuyển đổi dữ liệu ngày và giờ

Mới trong phiên bản 3. 6. Đã thêm đối số

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
96.

ngày giờ. múi giờ(tz=Không có)

Return a object with new attribute tz, adjusting the date and time data so the result is the same UTC time as self, but in tz’s local time

Nếu được cung cấp, tz phải là một thể hiện của một lớp con và các phương thức and của nó không được trả về

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03. Nếu bản thân là ngây thơ, nó được coi là đại diện cho thời gian trong múi giờ hệ thống

Nếu được gọi mà không có đối số (hoặc với

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
60), múi giờ cục bộ của hệ thống được giả định cho múi giờ đích. Thuộc tính
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
61 của phiên bản datetime đã chuyển đổi sẽ được đặt thành phiên bản có tên vùng và phần bù thu được từ HĐH

Nếu

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
63 là tz, thì
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
64 là tự. không thực hiện điều chỉnh dữ liệu ngày hoặc giờ. Mặt khác, kết quả là giờ địa phương trong múi giờ tz, biểu thị cùng thời gian UTC với chính mình. sau
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
65,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
66 sẽ có cùng dữ liệu ngày và giờ như
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
67

Nếu bạn chỉ muốn đính kèm một đối tượng múi giờ tz vào một datetime dt mà không cần điều chỉnh dữ liệu ngày và giờ, hãy sử dụng

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
68. Nếu bạn chỉ muốn xóa đối tượng múi giờ khỏi dt datetime nhận biết mà không chuyển đổi dữ liệu ngày và giờ, hãy sử dụng
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
69

Lưu ý rằng phương thức mặc định có thể được ghi đè trong một lớp con để ảnh hưởng đến kết quả được trả về bởi. Bỏ qua các trường hợp lỗi, hoạt động như

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2

Đã thay đổi trong phiên bản 3. 3. tz bây giờ có thể được bỏ qua.

Đã thay đổi trong phiên bản 3. 6. Phương thức hiện có thể được gọi trên các phiên bản ngây thơ được cho là đại diện cho giờ địa phương của hệ thống.

ngày giờ. utcoffset()

Nếu là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, nếu không thì trả về
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
78 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một đối tượng có cường độ nhỏ hơn một ngày

Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

ngày giờ. dst()

Nếu là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, nếu không thì trả về
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
84 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một đối tượng có cường độ nhỏ hơn một ngày

Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.

ngày giờ. tzname()

Nếu là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, ngược lại trả về
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
90, đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một đối tượng chuỗi,

ngày giờ. thời gian()

Trả lại một chẳng hạn như được trả lại bởi

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
52 tương đương với

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
3

trong đó

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
53 là số ngày trong năm hiện tại bắt đầu bằng
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
5 cho ngày 1 tháng 1. Cờ
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 của kết quả được thiết lập theo phương thức. là
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 được đặt thành
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
04;

ngày giờ. utctimetuple()

Nếu trường hợp d là ngây thơ, thì trường hợp này cũng giống như trường hợp

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
52 ngoại trừ trường hợp
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 bị buộc về 0 bất kể điều gì mà
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
13 trả về. DST không bao giờ có hiệu lực trong thời gian UTC

Nếu d biết, d được chuẩn hóa thành thời gian UTC, bằng cách trừ đi

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
14 và a cho thời gian chuẩn hóa được trả về.
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 buộc phải 0. Lưu ý rằng an có thể tăng nếu d. năm là
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
4 hoặc
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
8 và sự điều chỉnh của UTC tràn qua ranh giới năm

Cảnh báo

Bởi vì các đối tượng

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 ngây thơ được nhiều phương thức
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC; . Nếu bạn có một
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 ngây thơ đại diện cho UTC, hãy sử dụng
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
24 để làm cho nó biết, tại thời điểm đó bạn có thể sử dụng

ngày giờ. bình thường()

Trả về thứ tự Gregorian proleptic của ngày. Giống như

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
26

ngày giờ. dấu thời gian()

Trả về dấu thời gian POSIX tương ứng với phiên bản. Giá trị trả về tương tự như giá trị được trả về bởi

Các phiên bản ngây thơ được giả định là đại diện cho giờ địa phương và phương pháp này dựa vào hàm C

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
31 của nền tảng để thực hiện chuyển đổi. Vì hỗ trợ phạm vi giá trị rộng hơn
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
31 trên nhiều nền tảng nên phương pháp này có thể tăng trong thời gian xa trong quá khứ hoặc xa trong tương lai

Đối với các trường hợp nhận biết, giá trị trả về được tính là

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
4

Mới trong phiên bản 3. 3

Đã thay đổi trong phiên bản 3. 6. Phương thức sử dụng thuộc tính để phân biệt thời gian trong một khoảng thời gian lặp lại.

Ghi chú

Không có phương pháp nào để lấy dấu thời gian POSIX trực tiếp từ một phiên bản ngây thơ biểu thị thời gian UTC. Nếu ứng dụng của bạn sử dụng quy ước này và múi giờ hệ thống của bạn không được đặt thành UTC, thì bạn có thể lấy dấu thời gian POSIX bằng cách cung cấp

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
39

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
5

hoặc bằng cách tính toán dấu thời gian trực tiếp

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6

ngày giờ. ngày trong tuần()

Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. Giống như

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
40. See also

ngày giờ. ngày trong tuần()

Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 1 và Chủ Nhật là 7. Giống như

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
42. Xem thêm ,

ngày giờ. isocalendar()

Trả về a với ba thành phần.

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
1,
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
63 và
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
64. Giống như
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
48

ngày giờ. isoformat(sep=', timespec='auto')

Trả về một chuỗi biểu thị ngày và giờ ở định dạng ISO 8601

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    49, nếu không phải là 0

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    51, nếu là 0

Nếu không trả về

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, một chuỗi sẽ được thêm vào, tạo ra phần bù UTC

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    55, nếu không phải là 0

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    57, nếu là 0

ví dụ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
7

Đối số tùy chọn sep (mặc định

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
59) là dấu phân tách một ký tự, được đặt giữa phần ngày và giờ của kết quả. Ví dụ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8

Đối số tùy chọn timespec chỉ định số lượng thành phần bổ sung của thời gian cần đưa vào (mặc định là

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
60). Nó có thể là một trong những điều sau đây

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    60. Giống như
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    62 nếu là 0, giống như
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    64 nếu không

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    65. Bao gồm ở định dạng hai chữ số
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    67

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    68. Bao gồm và ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    71

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    62. Bao gồm , , và ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    76

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    77. Bao gồm toàn thời gian, nhưng cắt ngắn phần thứ hai thành mili giây. Định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    78

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    64. Bao gồm toàn thời gian ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    80

Ghi chú

Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn

sẽ được nâng lên trên một đối số timespec không hợp lệ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9

Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian.

ngày giờ. __str__()

Đối với trường hợp d,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
66 tương đương với
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
84

ngày giờ. thời gian()

Trả về một chuỗi đại diện cho ngày và giờ

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
0

Chuỗi đầu ra sẽ không bao gồm thông tin múi giờ, bất kể đầu vào là nhận biết hay ngây thơ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
68 tương đương với

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4

trên các nền tảng có chức năng C

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
69 gốc (gọi, nhưng không gọi) tuân theo tiêu chuẩn C

ngày giờ. thời gian chạy(định dạng)

Trả về một chuỗi đại diện cho ngày và giờ, được kiểm soát bởi một chuỗi định dạng rõ ràng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

ngày giờ. __format__(định dạng)

Giống như. Điều này giúp có thể chỉ định một chuỗi định dạng cho một đối tượng trong và khi sử dụng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

Ví dụ về cách sử dụng.

Ví dụ về làm việc với các đối tượng

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2

Ví dụ bên dưới định nghĩa một lớp con thu thập thông tin múi giờ cho Kabul, Afghanistan, sử dụng +4 UTC cho đến năm 1945 và sau đó là +4. 30 UTC sau đó

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
3

Cách sử dụng của

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
95 từ phía trên

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
4

Objects

Một đối tượng đại diện cho thời gian (cục bộ) trong ngày, không phụ thuộc vào bất kỳ ngày cụ thể nào và có thể điều chỉnh thông qua một đối tượng

class datetime. thời gian(giờ=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

Tất cả các đối số là tùy chọn. tzinfo có thể là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một thể hiện của lớp con. Các đối số còn lại phải là số nguyên trong các phạm vi sau

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    90,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    91,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    92,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    93,

  • >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    94

If an argument outside those ranges is given, is raised. All default to

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
14 except tzinfo, which defaults to

Class attributes

thời gian. phút

The earliest representable ,

>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
10

thời gian. tối đa

Đại diện mới nhất ,

>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
12

time. resolution

The smallest possible difference between non-equal objects,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
24, although note that arithmetic on objects is not supported

Instance attributes (read-only)

time. hour

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
96

time. minute

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
97

time. second

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
97

time. microsecond

Trong

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
99

time. tzinfo

Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 nếu không có đối số nào được truyền

time. fold

Trong

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. (Một khoảng thời gian lặp lại xảy ra khi đồng hồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ) Giá trị 0 (1) thể hiện sớm hơn (muộn hơn) của hai thời điểm có cùng biểu diễn thời gian tường

Mới trong phiên bản 3. 6

objects support comparison of to , where a is considered less than b when a precedes b in time. If one comparand is naive and the other is aware, is raised if an order comparison is attempted. For equality comparisons, naive instances are never equal to aware instances

If both comparands are aware, and have the same attribute, the common attribute is ignored and the base times are compared. If both comparands are aware and have different attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
30). In order to stop mixed-type comparisons from falling back to the default comparison by object address, when a object is compared to an object of a different type, is raised unless the comparison is
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
75 or
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
76. The latter cases return or , respectively

Đã thay đổi trong phiên bản 3. 3. So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ không nâng cao.

In Boolean contexts, a object is always considered to be true

Changed in version 3. 5. Before Python 3. 5, a object was considered to be false if it represented midnight in UTC. This behavior was considered obscure and error-prone and has been removed in Python 3. 5. See bpo-13936 for full details.

Other constructor

classmethod time. fromisoformat(time_string)

Return a corresponding to a time_string in any valid ISO 8601 format, with the following exceptions

  1. Độ lệch múi giờ có thể có giây phân số

  2. The leading

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    80, normally required in cases where there may be ambiguity between a date and a time, is not required

  3. Fractional seconds may have any number of digits (anything beyond 6 will be truncated)

  4. Giờ và phút phân số không được hỗ trợ

ví dụ

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5

Mới trong phiên bản 3. 7

Changed in version 3. 11. Previously, this method only supported formats that could be emitted by .

Phương thức sơ thẩm

time. replace(hour=self. hour , minute=self. minute , second=self. second , microsecond=self. microsecond , tzinfo=self. tzinfo , * , fold=0)

Return a with the same value, except for those attributes given new values by whichever keyword arguments are specified. Note that

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
52 can be specified to create a naive from an aware , without conversion of the time data

Mới trong phiên bản 3. 6. Đã thêm đối số

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
96.

time. isoformat(timespec='auto')

Return a string representing the time in ISO 8601 format, one of

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    80, if is not 0

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    76, if is 0

  • >>> # Components of another_year add up to exactly 365 days
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> another_year = timedelta(weeks=40, days=84, hours=23,
    ..                          minutes=50, seconds=600)
    >>> year == another_year
    True
    >>> year.total_seconds()
    31536000.0
    
    53, if does not return
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

  • >>> # Components of another_year add up to exactly 365 days
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> another_year = timedelta(weeks=40, days=84, hours=23,
    ..                          minutes=50, seconds=600)
    >>> year == another_year
    True
    >>> year.total_seconds()
    31536000.0
    
    56, if is 0 and does not return
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03

Đối số tùy chọn timespec chỉ định số lượng thành phần bổ sung của thời gian cần đưa vào (mặc định là

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
60). Nó có thể là một trong những điều sau đây

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    60. Giống như
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    62 nếu là 0, giống như
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    64 nếu không

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    65. Bao gồm ở định dạng hai chữ số
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    67

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    68. Bao gồm và ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    71

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    62. Bao gồm , , và ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    76

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    77. Bao gồm toàn thời gian, nhưng cắt ngắn phần thứ hai thành mili giây. Định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    78

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    64. Bao gồm toàn thời gian ở định dạng
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    80

Ghi chú

Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn

will be raised on an invalid timespec argument

Ví dụ

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
6

Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian.

time. __str__()

For a time t,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
55 is equivalent to
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
83

time. strftime(format)

Return a string representing the time, controlled by an explicit format string. For a complete list of formatting directives, see

time. __format__(format)

Giống như. Điều này giúp có thể chỉ định một chuỗi định dạng cho một đối tượng trong và khi sử dụng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem

time. utcoffset()

If is

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, returns
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, else returns
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
90, and raises an exception if the latter doesn’t return
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 or a object with magnitude less than one day

Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

time. dst()

Nếu là

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, nếu không thì trả về
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
96 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 hoặc một đối tượng có cường độ nhỏ hơn một ngày

Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.

time. tzname()

If is

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, returns
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, else returns
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
02, or raises an exception if the latter doesn’t return
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 or a string object

Ví dụ về cách sử dụng.

Examples of working with a object

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
7

Objects

lớp ngày giờ. tzinfo

This is an abstract base class, meaning that this class should not be instantiated directly. Define a subclass of to capture information about a particular time zone

An instance of (a concrete subclass of) can be passed to the constructors for and objects. The latter objects view their attributes as being in local time, and the object supports methods revealing offset of local time from UTC, the name of the time zone, and DST offset, all relative to a date or time object passed to them

You need to derive a concrete subclass, and (at least) supply implementations of the standard methods needed by the methods you use. The module provides , a simple concrete subclass of which can represent timezones with fixed offset from UTC such as UTC itself or North American EST and EDT

Special requirement for pickling. A subclass must have an

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
18 method that can be called with no arguments, otherwise it can be pickled but possibly not unpickled again. This is a technical requirement that may be relaxed in the future

A concrete subclass of may need to implement the following methods. Exactly which methods are needed depends on the uses made of aware objects. If in doubt, simply implement all of them

tzinfo. utcoffset(dt)

Return offset of local time from UTC, as a object that is positive east of UTC. If local time is west of UTC, this should be negative

This represents the total offset from UTC; for example, if a object represents both time zone and DST adjustments, should return their sum. If the UTC offset isn’t known, return

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03. Else the value returned must be a object strictly between
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
26 and
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
27 (the magnitude of the offset must be less than one day). Most implementations of will probably look like one of these two

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
8

If does not return

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, should not return
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 either

The default implementation of raises

Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

tzinfo. dst(dt)

Return the daylight saving time (DST) adjustment, as a object or

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 if DST information isn’t known

Return

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
83 if DST is not in effect. If DST is in effect, return the offset as a object (see for details). Note that DST offset, if applicable, has already been added to the UTC offset returned by , so there’s no need to consult unless you’re interested in obtaining DST info separately. For example, calls its attribute’s method to determine how the
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 flag should be set, and calls to account for DST changes when crossing time zones

An instance tz of a subclass that models both standard and daylight times must be consistent in this sense

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
49

must return the same result for every dt with

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
51 For sane subclasses, this expression yields the time zone’s “standard offset”, which should not depend on the date or the time, but only on geographic location. The implementation of relies on this, but cannot detect violations; it’s the programmer’s responsibility to ensure it. If a subclass cannot guarantee this, it may be able to override the default implementation of to work correctly with
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 regardless

Most implementations of will probably look like one of these two

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
9

or

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
0

The default implementation of raises

Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.

tzinfo. tzname(dt)

Return the time zone name corresponding to the object dt, as a string. Nothing about string names is defined by the module, and there’s no requirement that it mean anything in particular. For example, “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” are all valid replies. Return

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 if a string name isn’t known. Note that this is a method rather than a fixed string primarily because some subclasses will wish to return different names depending on the specific value of dt passed, especially if the class is accounting for daylight time

The default implementation of raises

These methods are called by a or object, in response to their methods of the same names. A object passes itself as the argument, and a object passes

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 as the argument. A subclass’s methods should therefore be prepared to accept a dt argument of
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03, or of class

When

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 is passed, it’s up to the class designer to decide the best response. For example, returning
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03 is appropriate if the class wishes to say that time objects don’t participate in the protocols. It may be more useful for
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
78 to return the standard UTC offset, as there is no other convention for discovering the standard offset

When a object is passed in response to a method,

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
81 is the same object as self. các phương thức có thể dựa vào điều này, trừ khi mã người dùng gọi trực tiếp các phương thức. Mục đích là các phương thức diễn giải dt theo giờ địa phương và không cần lo lắng về các đối tượng trong các múi giờ khác

There is one more method that a subclass may wish to override

tzinfo. fromutc(dt)

This is called from the default implementation. Khi được gọi từ đó,

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
81 là chính nó và dữ liệu ngày và giờ của dt sẽ được xem là thể hiện thời gian UTC. Mục đích của là điều chỉnh dữ liệu ngày và giờ, trả về một ngày giờ tương đương theo giờ địa phương của chính bạn

Most subclasses should be able to inherit the default implementation without problems. Nó đủ mạnh để xử lý các múi giờ có độ lệch cố định và các múi giờ tính cả thời gian tiêu chuẩn và thời gian ban ngày, và cả thời gian sau ngay cả khi thời gian chuyển đổi DST khác nhau trong các năm khác nhau. Một ví dụ về múi giờ mà việc triển khai mặc định có thể không xử lý chính xác trong mọi trường hợp là trường hợp mà độ lệch tiêu chuẩn (từ UTC) phụ thuộc vào ngày và giờ cụ thể đã trôi qua, điều này có thể xảy ra vì lý do chính trị. Việc triển khai mặc định của

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 và có thể không tạo ra kết quả bạn muốn nếu kết quả là một trong những giờ diễn ra vào thời điểm giá trị bù tiêu chuẩn thay đổi

Bỏ qua mã cho các trường hợp lỗi, việc triển khai mặc định hoạt động như

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
1

Trong tệp

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
95 sau đây có một số ví dụ về các lớp

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
2

Lưu ý rằng có những sự tinh tế không thể tránh khỏi hai lần mỗi năm trong một phân lớp chiếm cả thời gian tiêu chuẩn và thời gian ban ngày, tại các điểm chuyển tiếp DST. Để cụ thể, hãy xem xét Miền Đông Hoa Kỳ (UTC -0500), nơi EDT bắt đầu sau 1 phút. 59 (EST) vào Chủ nhật thứ hai của tháng 3 và kết thúc vào phút sau 1. 59 (EDT) on the first Sunday in November

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
3

When DST starts (the “start” line), the local wall clock leaps from 1. 59 to 3. 00. A wall time of the form 2. MM doesn’t really make sense on that day, so

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
98 won’t deliver a result with
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
99 on the day DST begins. For example, at the Spring forward transition of 2016, we get

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
4

When DST ends (the “end” line), there’s a potentially worse problem. there’s an hour that can’t be spelled unambiguously in local wall time. the last hour of daylight time. In Eastern, that’s times of the form 5. MM UTC on the day daylight time ends. The local wall clock leaps from 1. 59 (daylight time) back to 1. 00 (standard time) again. Local times of the form 1. MM are ambiguous.

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 mimics the local clock’s behavior by mapping two adjacent UTC hours into the same local hour then. In the Eastern example, UTC times of the form 5. MM and 6. MM both map to 1. MM when converted to Eastern, but earlier times have the attribute set to 0 and the later times have it set to 1. For example, at the Fall back transition of 2016, we get

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
5

Note that the instances that differ only by the value of the attribute are considered equal in comparisons

Applications that can’t bear wall-time ambiguities should explicitly check the value of the attribute or avoid using hybrid subclasses; there are no ambiguities when using , or any other fixed-offset subclass (such as a class representing only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours))

Xem thêm

The module has a basic class (for handling arbitrary fixed offsets from UTC) and its attribute (a UTC timezone instance)

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
1 brings the IANA timezone database (also known as the Olson database) to Python, and its usage is recommended

IANA timezone database

The Time Zone Database (often called tz, tzdata or zoneinfo) contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules

Objects

The class is a subclass of , each instance of which represents a timezone defined by a fixed offset from UTC

Objects of this class cannot be used to represent timezone information in the locations where different offsets are used in different days of the year or where historical changes have been made to civil time

class datetime. timezone(offset , name=None)

The offset argument must be specified as a object representing the difference between the local time and UTC. It must be strictly between

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
26 and
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
27, otherwise is raised

The name argument is optional. If specified it must be a string that will be used as the value returned by the method

Mới trong phiên bản 3. 2

Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

timezone. utcoffset(dt)

Return the fixed value specified when the instance is constructed

The dt argument is ignored. The return value is a instance equal to the difference between the local time and UTC

Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

timezone. tzname(dt)

Return the fixed value specified when the instance is constructed

If name is not provided in the constructor, the name returned by

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
24 is generated from the value of the
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
25 as follows. If offset is
>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
83, the name is “UTC”, otherwise it is a string in the format
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
27, where ± is the sign of
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
25, HH and MM are two digits of
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
29 and
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
30 respectively

Changed in version 3. 6. Name generated from

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
31 is now plain
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
32, not
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
33.

timezone. dst(dt)

Always returns

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
03

timezone. fromutc(dt)

Return

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
35. The dt argument must be an aware instance, with
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
4 set to
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
38

Class attributes

timezone. utc

The UTC timezone,

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
39

>>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 40 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 41 Behavior

, , and objects all support a

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
45 method, to create a string representing the time under the control of an explicit format string

Conversely, the class method creates a object from a string representing a date and time and a corresponding format string

The table below provides a high-level comparison of

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
40 versus
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
41

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
50

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
51

Usage

Convert object to a string according to a given format

Parse a string into a object given a corresponding format

Type of method

Instance method

Class method

Phương pháp của

; ;

Signature

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
45

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
58

>>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 40 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 41 Format Codes

The following is a list of all the format codes that the 1989 C standard requires, and these work on all platforms with a standard C implementation

Directive

Meaning

Example

Notes

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
61

Weekday as locale’s abbreviated name

Sun, Mon, …, Sat (en_US);

So, Mo, …, Sa (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
62

Weekday as locale’s full name

Sunday, Monday, …, Saturday (en_US);

Sonntag, Montag, …, Samstag (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
63

Weekday as a decimal number, where 0 is Sunday and 6 is Saturday

0, 1, …, 6

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
64

Day of the month as a zero-padded decimal number

01, 02, …, 31

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
65

Month as locale’s abbreviated name

Jan, Feb, …, Dec (en_US);

Jan, Feb, …, Dez (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
66

Month as locale’s full name

January, February, …, December (en_US);

Januar, Februar, …, Dezember (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
67

Month as a zero-padded decimal number

01, 02, …, 12

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
68

Year without century as a zero-padded decimal number

00, 01, …, 99

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
69

Year with century as a decimal number

0001, 0002, …, 2013, 2014, …, 9998, 9999

(2)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
70

Hour (24-hour clock) as a zero-padded decimal number

00, 01, …, 23

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
71

Hour (12-hour clock) as a zero-padded decimal number

01, 02, …, 12

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
72

Locale’s equivalent of either AM or PM

AM, PM (en_US);

am, pm (de_DE)

(1), (3)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
73

Minute as a zero-padded decimal number

00, 01, …, 59

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
74

Second as a zero-padded decimal number

00, 01, …, 59

(4), (9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
75

Microsecond as a decimal number, zero-padded to 6 digits

000000, 000001, …, 999999

(5)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
76

Phần bù UTC ở dạng

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
77 (chuỗi trống nếu đối tượng là ngây thơ)

(trống), +0000, -0400, +1030, +063415, -030712. 345216

(6)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
78

Time zone name (empty string if the object is naive)

(empty), UTC, GMT

(6)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
79

Day of the year as a zero-padded decimal number

001, 002, …, 366

(9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
80

Số tuần của năm (Chủ nhật là ngày đầu tiên của tuần) dưới dạng số thập phân không đệm. All days in a new year preceding the first Sunday are considered to be in week 0

00, 01, …, 53

(7), (9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
81

Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0

00, 01, …, 53

(7), (9)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
82

Locale’s appropriate date and time representation

Tue Aug 16 21. 30. 00 1988 (en_US);

Di 16 Aug 21. 30. 00 1988 (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
83

Locale’s appropriate date representation

08/16/88 (None);

08/16/1988 (en_US);

16. 08. 1988 (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
84

Locale’s appropriate time representation

21. 30. 00 (en_US);

21. 30. 00 (de_DE)

(1)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
85

A literal

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
86 character

%

Several additional directives not required by the C89 standard are included for convenience. These parameters all correspond to ISO 8601 date values

Directive

Meaning

Example

Notes

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
87

ISO 8601 year with century representing the year that contains the greater part of the ISO week (

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
88)

0001, 0002, …, 2013, 2014, …, 9998, 9999

(8)

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
89

ISO 8601 weekday as a decimal number where 1 is Monday

1, 2, …, 7

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
88

ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4

01, 02, …, 53

(8), (9)

These may not be available on all platforms when used with the

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
40 method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
41 with incomplete or ambiguous ISO 8601 directives will raise a

The full set of format codes supported varies across platforms, because Python calls the platform C library’s

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
40 function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. There are also differences between platforms in handling of unsupported format specifiers

New in version 3. 6.

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
87,
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
89 and
>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
88 were added.

Technical Detail

Broadly speaking,

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
98 acts like the module’s
>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
00 although not all objects support a
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
42 method

For the class method, the default value is

>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
03. any components not specified in the format string will be pulled from the default value.

Using

>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
04 is equivalent to

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
1

except when the format includes sub-second components or timezone offset information, which are supported in

>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
05 but are discarded by
>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
06

For objects, the format codes for year, month, and day should not be used, as objects have no such values. If they’re used anyway,

>>> from datetime import date
>>> d = date(2002, 12, 31)
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
09 is substituted for the year, and
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600)
>>> year == another_year
True
>>> year.total_seconds()
31536000.0
5 for the month and day

For objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as objects have no such values. If they’re used anyway,

>>> from datetime import timedelta
>>> delta = timedelta(
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
14 is substituted for them

For the same reason, handling of format strings containing Unicode code points that can’t be represented in the charset of the current locale is also platform-dependent. On some platforms such code points are preserved intact in the output, while on others

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('20191204')
datetime.date(2019, 12, 4)
>>> date.fromisoformat('2021-W01-1')
datetime.date(2021, 1, 4)
50 may raise or return an empty string instead

ghi chú

  1. Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, “month/day/year” versus “day/month/year”), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is

    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    16, the default encoding could be any one of
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    17,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    18, or
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    19; use to determine the current locale’s encoding)

  2. The

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method can parse years in the full [1, 9999] range, but years < 1000 must be zero-filled to 4-digit width

    Changed in version 3. 2. In previous versions,

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    40 method was restricted to years >= 1900.

    Changed in version 3. 3. In version 3. 2,

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    40 method was restricted to years >= 1000.

  3. When used with the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method, the
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    72 directive only affects the output hour field if the
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    71 directive is used to parse the hour

  4. Unlike the module, the module does not support leap seconds

  5. When used with the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method, the
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    75 directive accepts from one to six digits and zero pads on the right.
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    75 là một phần mở rộng cho tập hợp các ký tự định dạng trong tiêu chuẩn C (nhưng được triển khai riêng trong các đối tượng ngày giờ và do đó luôn có sẵn)

  6. For a naive object, the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76 and
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78 format codes are replaced by empty strings

    For an aware object

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57 is transformed into a string of the form
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    77, where
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    67 is a 2-digit string giving the number of UTC offset hours,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    38 is a 2-digit string giving the number of UTC offset minutes,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    39 is a 2-digit string giving the number of UTC offset seconds and
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 is a 6-digit string giving the number of UTC offset microseconds. The
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 part is omitted when the offset is a whole number of seconds and both the
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 and the
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    39 part is omitted when the offset is a whole number of minutes. For example, if
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57 returns
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    45,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76 is replaced with the string
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    47

    Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.

    Changed in version 3. 7. When the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76 directive is provided to the
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    50 will be parsed as an offset of one hour. In addition, providing
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    51 is identical to
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    52.

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78

    In

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    40,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78 is replaced by an empty string if
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> ten_years = 10 * year
    >>> ten_years
    datetime.timedelta(days=3650)
    >>> ten_years.days // 365
    10
    >>> nine_years = ten_years - year
    >>> nine_years
    datetime.timedelta(days=3285)
    >>> three_years = nine_years // 3
    >>> three_years, three_years.days // 365
    (datetime.timedelta(days=1095), 3)
    
    65 returns
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    03; otherwise
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78 is replaced by the returned value, which must be a string

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 only accepts certain values for
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78

    1. any value in

      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      61 for your machine’s locale

    2. the hard-coded values

      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      62 and
      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      63

    Vì vậy, một người nào đó sống ở Nhật Bản có thể có các giá trị hợp lệ là

    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    64,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    62 và
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    63, nhưng có lẽ không phải là
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    67. It will raise
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    93 for invalid values

    Changed in version 3. 2. When the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76 directive is provided to the
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method, an aware object will be produced. The
    >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    4 of the result will be set to a instance.

  7. Khi được sử dụng với phương pháp

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    80 và
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    81 chỉ được sử dụng trong các phép tính khi ngày trong tuần và năm dương lịch (
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    69) được chỉ định

  8. Similar to

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    80 and
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    81,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    88 is only used in calculations when the day of the week and the ISO year (
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    87) are specified in a
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 format string. Also note that
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    87 and
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    69 are not interchangeable

  9. When used with the

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    41 method, the leading zero is optional for formats
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    64,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    67,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    70,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    71,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    73,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    74,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    79,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    80,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    81, and
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    88. Format
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    68 does require a leading zero

Footnotes

If, that is, we ignore the effects of Relativity

This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations, where it’s the base calendar for all computations. See the book for algorithms for converting between proleptic Gregorian ordinals and many other calendar systems