Làm cách nào để in ngày utc trong python?

Giả sử chúng ta có một đối tượng datetime.datetime đại diện cho một thời điểm cụ thể [theo múi giờ địa phương của máy]

>>> import datetime
>>> april_fools = datetime.datetime[2030, 4, 1, 10, 0]

Biến april_fools đó trỏ đến một đối tượng datetime đại diện cho 10 giờ sáng hoặc ngày 1 tháng 4 năm 2030 [theo múi giờ địa phương của chúng tôi]

>>> april_fools
datetime.datetime[2030, 4, 1, 10, 0]

Để chuyển đổi thời gian này thành UTC, chúng ta có thể sử dụng phương thức astimezone trên đối tượng datetime.datetime của mình

>>> utc_april_fools = april_fools.astimezone[datetime.timezone.utc]
>>> utc_april_fools
datetime.datetime[2030, 4, 1, 17, 0, tzinfo=datetime.timezone.utc]
>>> print[utc_april_fools]
2030-04-01 17:00:00+00:00

Nếu chúng ta muốn chuyển đổi thời gian hiện tại sang UTC thì sao?

Trong khi các đối tượng datetime của Python có một

>>> datetime.datetime.utcnow[]
datetime.datetime[2030, 4, 1, 8, 15, 59, 89013]

Tài liệu Python khuyên bạn nên chuyển múi giờ đích [

>>> april_fools
datetime.datetime[2030, 4, 1, 10, 0]
2 trong trường hợp của chúng tôi] sang múi giờ thay thế

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

mô-đun

Các chức năng liên quan đến lịch chung

mô-đun

Thời gian truy cập và chuyển đổi

mô-đun

Múi giờ cụ thể đại diện cho cơ sở dữ liệu múi giờ IANA

ngày đóng gói

Thư viện của bên thứ ba với múi giờ mở rộng và hỗ trợ phân tích cú pháp

Đối tượng nhận thức và ngây thơ

Các đối tượng ngày và giờ có thể được phân loại là "nhận biết" hoặc "ngây thơ" tùy thuộc vào việc chúng có bao gồm thông tin múi giờ hay không

Với kiến ​​thức đầy đủ về các điều chỉnh thời gian chính trị và thuật toán hiện hành, chẳng hạn như múi giờ và thông tin về thời gian tiết kiệm ánh sáng ban ngày, một đối tượng nhận biết có thể định vị chính nó so với các đối tượng nhận biết khác. Một đối tượng nhận thức đại diện cho một thời điểm cụ thể không thể giải thích được.

Một đối tượng ngây thơ không chứa đủ thông tin để định vị chính nó một cách rõ ràng so với các đối tượng ngày/giờ khác. Việc một đối tượng ngây thơ đại diện cho Giờ phối hợp quốc tế [UTC], giờ địa phương hay thời gian ở một số múi giờ khác hoàn toàn phụ thuộc vào chương trình, giống như việc một số cụ thể đại diện cho mét, dặm hay khối lượng hay không là tùy thuộc vào chương trình. Các đối tượng ngây thơ dễ hiểu và dễ làm việc với cái giá phải trả là bỏ qua một số khía cạnh của thực tế

Đối với các ứng dụng yêu cầu các đối tượng nhận biết và các đối tượng có thuộc tính thông tin múi giờ tùy chọ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'
4, có thể được đặt thành một thể hiện của một lớp con của lớp trừu tượng. Các đối tượng này nắm bắt thông tin về phần bù từ thời gian UTC, tên múi giờ và liệu thời gian tiết kiệm ánh sáng ban ngày có hiệu lực hay không

Chỉ có một lớp cụ thể, lớp, được cung cấp bởi mô-đun. Lớp này có thể đại diện cho các múi giờ đơn giản với độ lệch cố định từ UTC, chẳng hạn như chính UTC hoặc các múi giờ EST và EDT của Bắc Mỹ. Hỗ trợ các múi giờ ở mức độ chi tiết sâu hơn tùy thuộc vào ứng dụng. Các quy tắc điều chỉnh thời gian trên toàn thế giới mang tính chính trị hơn là hợp lý, thay đổi thường xuyên và không có tiêu chuẩn nào phù hợp cho mọi ứng dụng ngoài UTC

hằng số

Mô-đun xuất các hằng số sau

ngày giờ. MIN NĂM

Số năm nhỏ nhất được phép trong một hoặc đối tượng. 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
5

ngày giờ. TỐI ĐA NĂM

Số năm lớn nhất được phép trong một hoặc đối tượng. 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
9

ngày giờ. UTC

Bí danh cho múi giờ UTC đơn lẻ

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

Các loại có sẵn

lớp ngày giờ. ngày

Một ngày ngây thơ được lý tưởng hóa, giả sử rằng lịch Gregorian hiện tại luôn có hiệu lực. Thuộc tính. , , và

lớp ngày giờ. thời gian

Thời gian lý tưởng hóa, không phụ thuộc vào bất kỳ ngày cụ thể nào, giả sử rằng mỗi ngày có chính xác 24*60*60 giây. [Không có khái niệm “giây nhuận” ở đây. ] Thuộc tính. , , , , và

lớp ngày giờ. ngày giờ

Một sự kết hợp của một ngày và một thời gian. Thuộc tính. , , , , , , , và

lớp ngày giờ. đồng hồ thời gian

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

________số 8_______

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

Xác định xem một đối tượng là Aware hay Naive

Các đối tượng thuộc loại luôn ngây thơ

Một đối tượng thuộc loại hoặc có thể nhận thức hoặc ngây thơ

Một đối tượng d nhận biết nếu cả hai điều sau đây đều đúng

  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 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

  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 không trở lạ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

Mặt khác, d là ngây thơ

Một đối tượng t nhận biết nếu cả hai điều sau đây giữ

  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 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

  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 không trở lạ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

Nếu không, t là ngây thơ

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

Các đối tượng

Một đối tượng đại diện cho một khoảng thời gian, sự khác biệt giữa hai ngày hoặc thời gian

lớp ngày giờ. timedelta[ngày=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]

Tất cả các đối số là tùy chọn và mặc đị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]
14. Các đối số có thể là số nguyên hoặc số float và có thể dương hoặc âm

Chỉ ngày, giây và micro giây được lưu trữ nội bộ. Các đối số được chuyển đổi thành các đơn vị đó

  • Một phần nghìn giây được chuyển đổi thành 1000 micro giây

  • Một phút được chuyển thành 60 giây

  • Một giờ được chuyển đổi thành 3600 giây

  • Một tuần được chuyển đổi thành 7 ngày

và ngày, giây và micro giây sau đó được chuẩn hóa để biểu diễn là duy nhất, 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]
    
    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 [số giây trong một 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]
    
    17

Ví dụ sau đây minh họa cách mọi đối số ngoài ngày, giây và micro giây được “hợp nhất” và chuẩn hóa thành ba thuộc tính kết quả đó

>>> 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]

Nếu bất kỳ đối số nào là số float và có micro giây phân số, thì micro giây phân số còn lại từ tất cả các đối số sẽ được kết hợp và tổng của chúng được làm tròn đến micro giây gần nhất bằng cách sử dụng bộ ngắt kết nối từ nửa vòng đến chẵn. Nếu không có đối số nào là float, quá trình chuyển đổi và chuẩn hóa là chính xác [không có thông tin nào bị mất]

Nếu giá trị chuẩn hóa của ngày nằm ngoài phạm vi được chỉ định, được tăng lên

Lưu ý rằng việc chuẩn hóa các giá trị âm lúc đầu có thể gây ngạc nhiên. Ví dụ

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

Thuộc tính lớp

đồng bằng thời gian. phút

Đối tượng tiêu cực nhấ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]
20

đồng bằng thời gian. tối đa

Đối tượng tích cực nhấ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]
22

đồng bằng thời gian. độ phân giải

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

>>> 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

Lưu ý rằng, do chuẩn hó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]
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 không thể biểu diễn như một đối tượng

Thuộc tính phiên bản [chỉ đọc]

Thuộc tính

Giá trị

>>> 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

Giữa -999999999 và bao gồm cả 999999999

>>> 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

Bao gồm từ 0 đến 86399

>>> 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

Bao gồm từ 0 đến 999999

hoạt động được hỗ trợ

Hoạt động

Kết quả

>>> 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

Tổng của t2 và t3. Sau đó t1-t2 == t3 và t1-t3 == t2 là đúng. [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

Sự khác biệt của t2 và t3. Sau đó t1 == t2 - t3 và t2 == t1 + t3 là đúng. [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 nhân với một số nguyên. Sau đó t1 // i == t2 là đúng, với điều kiệ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]
35

Nói chung, t1 * i == t1 * [i-1] + t1 là đúng. [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]

Đã thay đổi trong phiên bản 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]

Các đối tượng

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à 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 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 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 ________9____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

Thuộc tính lớp

ngày. phút

Ngày có thể đạ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

Thuộc tính phiên bản [chỉ đọc]

ngày. năm

Giữa và bao gồm

ngày. tháng

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

hoạt động được hỗ trợ

Hoạt động

Kết quả

>>> 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 is considered less than date2 when date1 precedes date2 in time. [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 and
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    30 are ignored. đượ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. timedelta. giây và timedelta. micro giây là 0 và date2 + timedelta == date1 sau

  4. In other words,

    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    25 if and only if
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    38. Date comparison raises if the other comparand isn’t also a object. However,
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    41 is returned instead if the other comparand has a
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    42 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, 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

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

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

date. thay thế[năm=chính mình. year , month=self. month , day=self. 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

Thí 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

where

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
53 is the day number within the current year starting with
>>> # 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 January 1st

date. toordinal[]

Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any object d,

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

date. weekday[]

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 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

Các đối tượng

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 ____9_______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 bởi , 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 ________54____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. có thể trả về các trường hợp được đặt thành 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 chức năng 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
>>> 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

Để 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à bằng cách gọi
>>> 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. The non-date components of the datetime are populated with their normal default values. This is the inverse of the function

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

classmethod datetime. strptime[date_string , format]

Return a corresponding to date_string, parsed according to format

This is equivalent to

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

is raised if the date_string and format can’t be parsed by or if it returns a value which isn’t a time tuple. For a complete list of formatting directives, see

Thuộc tính lớp

datetime. min

The earliest representable ,

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

datetime. max

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

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

datetime. resolution

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

>>> 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

Thuộc tính phiên bản [chỉ đọc]

datetime. year

Giữa và bao gồm

datetime. month

Từ 1 đến 12 bao gồm

datetime. day

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

datetime. hour

In

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

datetime. minute

In

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

datetime. second

In

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

datetime. microsecond

In

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

datetime. tzinfo

The object passed as the tzinfo argument to the constructor, 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 none was passed

datetime. fold

In

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. Used to disambiguate wall times during a repeated interval. [A repeated interval occurs when clocks are rolled back at the end of daylight saving time or when the UTC offset for the current zone is decreased for political reasons. ] The value 0 [1] represents the earlier [later] of the two moments with the same wall time representation

New in version 3. 6

hoạt động được hỗ trợ

Hoạt động

Kết quả

>>> 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

Compares to . [4]

  1. datetime2 is a duration of timedelta removed from datetime1, moving forward in time if

    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    21 > 0, or backward if
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    21 < 0. The result has the same attribute as the input datetime, and datetime2 - datetime1 == timedelta after. is raised if datetime2. year would be smaller than or larger than . 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. Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same attribute as the input datetime, and no time zone adjustments are done even if the input is aware

  3. Subtraction of a from a is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive, is raised

    If both are naive, or both are aware and have the same attribute, the attributes are ignored, and the result is a object t such that

    >>> from datetime import timedelta
    >>> delta1 = timedelta[seconds=57]
    >>> delta2 = timedelta[hours=25, seconds=2]
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    22. No time zone adjustments are done in this case

    If both are aware and have different attributes,

    >>> from datetime import timedelta
    >>> delta1 = timedelta[seconds=57]
    >>> delta2 = timedelta[hours=25, seconds=2]
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    24 acts as if a and b were first converted to naive UTC datetimes first. The result is
    >>> from datetime import timedelta
    >>> delta1 = timedelta[seconds=57]
    >>> delta2 = timedelta[hours=25, seconds=2]
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    25 except that the implementation never overflows

  4. datetime1 is considered less than datetime2 when datetime1 precedes datetime2 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 datetimes 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]

    Changed in version 3. 3. Equality comparisons between aware and naive instances don’t raise .

    Note

    In order to stop comparison from falling back to the default scheme of comparing object addresses, datetime comparison normally raises if the other comparand isn’t also a object. However,

    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    41 is returned instead if the other comparand has a
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    42 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, 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

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

datetime. date[]

Return object with same year, month and day

datetime. time[]

Return object with same hour, minute, second, microsecond and fold. 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. See also method

Changed in version 3. 6. The fold value is copied to the returned object.

datetime. timetz[]

Return object with same hour, minute, second, microsecond, fold, and tzinfo attributes. See also method

Changed in version 3. 6. The fold value is copied to the returned object.

datetime. 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ó]

Trả về một đối tượng có thuộc tính mới tz, điều chỉnh dữ liệu ngày và giờ để kết quả có cùng thời gian UTC với chính nó, nhưng theo giờ địa phương của tz

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ờ địa phương 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ự. no adjustment of date or time data is performed. Else the result is local time in the timezone tz, representing the same UTC time as self. 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

If you merely want to attach a time zone object tz to a datetime dt without adjustment of date and time data, use

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
68. If you merely want to remove the time zone object from an aware datetime dt without conversion of date and time data, use
>>> 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

Changed in version 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

Changed in version 3. 7. DST offset không bị giới hạn trong một số phút.

ngày giờ. 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
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
90, 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,

datetime. 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

where

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
53 is the day number within the current year starting with
>>> # 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 January 1st. 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 đặt 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;

datetime. 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

If d is aware, d is normalized to UTC time, by subtracting

>>> 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, and a for the normalized time is returned.
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 bị ép thành 0. Note that an may be raised if d. year was
>>> # 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 or
>>> # 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 and UTC adjustment spills over a year boundary

Cảnh báo

Because naive

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 objects are treated by many
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 methods as local times, it is preferred to use aware datetimes to represent times in UTC; as a result, using
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
22 may give misleading results. If you have a naive
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 representing UTC, use
>>> 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 to make it aware, at which point you can use

datetime. toordinal[]

Return the proleptic Gregorian ordinal of the date. The same as

>>> 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

datetime. timestamp[]

Return POSIX timestamp corresponding to the instance. The return value is a similar to that returned by

Naive instances are assumed to represent local time and this method relies on the platform 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 function to perform the conversion. Since supports wider range of values than
>>> 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 on many platforms, this method may raise for times far in the past or far in the future

For aware instances, the return value is computed as

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

New in version 3. 3

Changed in version 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.

Note

There is no method to obtain the POSIX timestamp directly from a naive instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying

>>> 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

or by calculating the timestamp directly

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

datetime. weekday[]

Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as

>>> 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

datetime. isoweekday[]

Return the day of the week as an integer, where Monday is 1 and Sunday is 7. The same as

>>> 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. See also ,

datetime. isocalendar[]

Return a with three components.

>>> 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 and
>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
64. The same as
>>> 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

datetime. isoformat[sep='T' , timespec='auto']

Return a string representing the date and time in ISO 8601 format

  • >>> 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, 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'
    
    51, if is 0

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, a string is appended, giving the UTC offset

  • >>> 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, 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'
    
    57, if is 0

ví dụ

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

The optional argument sep [default

>>> 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] is a one-character separator, placed between the date and time portions of the result. For example

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

The optional argument timespec specifies the number of additional components of the time to include [the default is

>>> 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]. It can be one of the following

  • >>> 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. Same as
    >>> 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 if is 0, same as
    >>> 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 otherwise

  • >>> 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. Include the in the two-digit
    >>> 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 format

  • >>> 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. Include and in
    >>> 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 format

  • >>> 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. Include , , and in
    >>> 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 format

  • >>> 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.
    >>> 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 format

  • >>> 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

Note

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

Các đối tượng

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]

All arguments are optional. 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

Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên. Tất cả mặc đị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]
14 ngoại trừ tzinfo, mặc định là

Thuộc tính lớp

thời gian. phút

Đại diện sớm 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
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

thời gian. độ phân giải

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

>>> 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, mặc dù lưu ý rằng số học trên các đối tượng không được hỗ trợ

Thuộc tính phiên bản [chỉ đọc]

thời gian. giờ

In

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

time. minute

In

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

time. second

In

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

time. microsecond

In

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

time. tzinfo

The object passed as the tzinfo argument to the constructor, 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 none was passed

time. fold

In

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. Used to disambiguate wall times during a repeated interval. [A repeated interval occurs when clocks are rolled back at the end of daylight saving time or when the UTC offset for the current zone is decreased for political reasons. ] The value 0 [1] represents the earlier [later] of the two moments with the same wall time representation

New in version 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. Các trường hợp sau trả về hoặc , tương ứng

Changed in version 3. 3. Equality comparisons between aware and naive instances don’t raise .

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

The optional argument timespec specifies the number of additional components of the time to include [the default is

>>> 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]. It can be one of the following

  • >>> 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. Same as
    >>> 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 if is 0, same as
    >>> 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 otherwise

  • >>> 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. Include the in the two-digit
    >>> 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 format

  • >>> 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. Include and in
    >>> 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 format

  • >>> 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. Include , , and in
    >>> 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 format

  • >>> 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.
    >>> 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 format

  • >>> 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

Note

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

Thí 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[]

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
96, 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

Changed in version 3. 7. DST offset không bị giới hạn trong một số phút.

thời gian. 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.

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

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

Các đối tượng

lớp ngày giờ. tzinfo

Đây là một lớp cơ sở trừu tượng, có nghĩa là lớp này không được khởi tạo trực tiếp. Xác định một lớp con của để nắm bắt thông tin về một múi giờ cụ thể

An instance of [a concrete subclass of] can be passed to the constructors for and objects. Các đối tượng thứ hai xem các thuộc tính của chúng là theo giờ địa phương và đối tượng hỗ trợ các phương thức hiển thị phần bù giờ địa phương từ UTC, tên của múi giờ và phần bù DST, tất cả đều liên quan đến đối tượng ngày hoặc giờ được truyền cho chúng

Bạn cần lấy được một lớp con cụ thể và [ít nhất] cung cấp các triển khai của các phương thức tiêu chuẩn cần thiết cho các phương thức bạn sử dụng. Mô-đun cung cấp , một phân lớp cụ thể đơn giản trong đó có thể biểu thị các múi giờ với phần bù cố định từ UTC, chẳng hạn như chính UTC hoặc EST và EDT của Bắc Mỹ

Special requirement for pickling. Một lớp con phải có một phương thứ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]
18 có thể được gọi mà không có đối số, nếu không, nó có thể được chọn nhưng có thể không được giải nén lại. This is a technical requirement that may be relaxed in the future

A concrete subclass of may need to implement the following methods. Chính xác những phương pháp nào là cần thiết phụ thuộc vào việc sử dụng các đối tượng nhận biết. Nếu nghi ngờ, chỉ cần thực hiện tất cả chúng

tzinfo. utcoffset[dt]

Trả về phần bù của giờ địa phương từ UTC, dưới dạng một đối tượng dương ở phía đông của 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

Changed in version 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. methods can rely on this, unless user code calls methods directly. The intent is that the methods interpret dt as being in local time, and not need worry about objects in other timezones

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

tzinfo. fromutc[dt]

This is called from the default implementation. When called from that,

>>> 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 self, and dt’s date and time data are to be viewed as expressing a UTC time. The purpose of is to adjust the date and time data, returning an equivalent datetime in self’s local time

Most subclasses should be able to inherit the default implementation without problems. It’s strong enough to handle fixed-offset time zones, and time zones accounting for both standard and daylight time, and the latter even if the DST transition times differ in different years. An example of a time zone the default implementation may not handle correctly in all cases is one where the standard offset [from UTC] depends on the specific date and time passed, which can happen for political reasons. The default implementations of

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 and may not produce the result you want if the result is one of the hours straddling the moment the standard offset changes

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

In the following

>>> 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 file there are some examples of classes

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

Note that there are unavoidable subtleties twice per year in a subclass accounting for both standard and daylight time, at the DST transition points. For concreteness, consider US Eastern [UTC -0500], where EDT begins the minute after 1. 59 [EST] on the second Sunday in March, and ends the minute after 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

Các đối tượng

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.

múi giờ. 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.

múi giờ. tzname[dt]

Return the fixed value specified when the instance is constructed

Nếu tên không được cung cấp trong hàm tạo, tên được trả về bởi

>>> 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 được tạo từ giá trị củ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]
25 như sau. Nếu độ lệch 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]
83, tên là “UTC”, nếu không, nó là một chuỗi ở định 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]
27, trong đó ± là dấu củ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]
25, HH và MM lần lượt là hai chữ số củ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]
29 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]
30

Đã thay đổi trong phiên bản 3. 6. Tên được tạo từ

>>> 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 hiện là
>>> 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 đơn giản, không phải
>>> 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.

múi giờ. dst[dt]

Luôn 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úi giờ. từutc[dt]

Trả lại

>>> 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. Đối số dt phải là một trường hợp có nhận thức, với ____5_______4 được đặt thành _____80_______38

Thuộc tính lớp

múi giờ. utc

Múi giờ UTC,

>>> 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 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]
41 Hành vi

, , và các đối tượng đều hỗ trợ phương thức

>>> 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, để tạo chuỗi biểu thị thời gian dưới sự kiểm soát của chuỗi định dạng rõ ràng

Ngược lại, phương thức lớp tạo một đối tượng từ một chuỗi biểu thị ngày giờ và một chuỗi định dạng tương ứng

Bảng dưới đây cung cấp so sánh cấp cao giữ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]
40 so với
>>> 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

Cách sử dụng

Chuyển đổi đối tượng thành một chuỗi theo một định dạng nhất định

Phân tích một chuỗi thành một đối tượng có định dạng tương ứng

Type of method

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

phương pháp lớp

Phương pháp của

;

Chữ ký

>>> 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

Mã định 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]
40 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]
41

Sau đây là danh sách tất cả các mã định dạng mà tiêu chuẩn C 1989 yêu cầu và chúng hoạt động trên tất cả các nền tảng có triển khai C tiêu chuẩn

chỉ thị

Nghĩa

Example

ghi 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]
61

Ngày trong tuần là tên viết tắt của ngôn ngữ

CN, 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

Ngày trong tuần là tên đầy đủ của ngôn ngữ

Chủ Nhật, Thứ Hai, …, Thứ Bảy [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

Ngày trong tuần dưới dạng số thập phân, trong đó 0 là Chủ nhật và 6 là Thứ bảy

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

Ngày trong tháng dưới dạng số thập phân không đệm

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

Tháng là tên viết tắt của ngôn ngữ

Tháng 1, Tháng 2, …, Tháng 12 [en_US];

Tháng 1, Tháng 2, …, 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

Tháng là tên đầy đủ của ngôn ngữ

Tháng Giêng, Tháng Hai, …, Tháng Mười Hai [en_US];

Januar, Februar, …, Tháng mười hai [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

Tháng dưới dạng số thập phân không đệm

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

Năm với thế kỷ dưới dạng số thập phân

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

Giờ [đồng hồ 24 giờ] dưới dạng số thập phân không đệm

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

Giờ [đồng hồ 12 giờ] dưới dạng số thập phân không đệm

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

Ngôn ngữ tương đương với AM hoặc PM

AM, PM [en_US];

sáng, chiều [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

Micro giây dưới dạng số thập phân, được đệm bằng 0 thành 6 chữ số

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

Độ lệch 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

Tên múi giờ [chuỗi trống nếu đối tượng ngây thơ]

[trống], 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

Ngày trong năm dưới dạng số thập phân không đệm

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. Tất cả các ngày trong một năm mới trước Chủ nhật đầu tiên được coi là trong tuần 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. Tất cả các ngày trong năm mới trước ngày thứ Hai đầu tiên được coi là trong tuần 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

Đại diện ngày và giờ thích hợp của ngôn ngữ

Thứ ba 16 tháng 8 21. 30. 00 1988 [vi_Hoa Kỳ];

Di 16 Thg8 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

Đại diện ngày thích hợp của địa phương

16/08/88 [Không có];

16/08/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

Đại diện thời gian thích hợp của địa phương

21. 30. 00 [vi_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

Một ký tự

>>> 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 theo nghĩa đen

%

Một số chỉ thị bổ sung không bắt buộc theo tiêu chuẩn C89 được bao gồm để thuận tiện. Tất cả các tham số này đều tương ứng với các giá trị ngày theo tiêu chuẩn ISO 8601

chỉ thị

Nghĩa

Example

ghi 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]
87

Năm ISO 8601 với thế kỷ đại diện cho năm chứa phần lớn tuần ISO [

>>> 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

[số 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 ngày trong tuần dưới dạng số thập phân trong đó 1 là Thứ Hai

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 tuần dưới dạng số thập phân với Thứ Hai là ngày đầu tuần. Tuần 01 là tuần có ngày 04/01

01, 02, …, 53

[8], [9]

Những thứ này có thể không có sẵn trên tất cả các nền tảng 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]
40. Chỉ thị năm và tuần ISO 8601 không thể hoán đổi cho nhau với chỉ thị số năm và tuần ở trên. Gọi
>>> 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 với các chỉ thị ISO 8601 không đầy đủ hoặc mơ hồ sẽ làm tăng

Bộ mã định dạng đầy đủ được hỗ trợ khác nhau giữa các nền tảng, bởi vì Python gọi hàm

>>> 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 của thư viện C và các biến thể nền tảng là phổ biến. To see the full set of format codes supported on your platform, consult the strftime[3] documentation. Cũng có sự khác biệt giữa các nền tảng trong việc xử lý các thông số định dạng không được hỗ trợ

Mới trong phiên bản 3. 6. _______80_______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 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]
88 đã được thêm vào.

Chi tiết kỹ thuật

Nói chung,

>>> 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 hoạt động giống như mô-đun
>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
00 mặc dù không phải tất cả các đối tượng đều hỗ trợ phương thức
>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
42

Đối với phương thức của lớp, giá trị mặc định là

>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
03. bất kỳ thành phần nào không được chỉ định trong chuỗi định dạng sẽ được lấy từ giá trị mặc định.

Sử dụng

>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
04 tương đương với

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

ngoại trừ khi định dạng bao gồm các thành phần giây phụ hoặc thông tin bù múi giờ, được hỗ trợ trong

>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
05 nhưng bị loại bỏ bởi
>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
06

Đối với các đối tượng, không nên sử dụng các mã định dạng cho năm, tháng và ngày vì các đối tượng không có các giá trị đó. Nếu chúng vẫn được sử dụng, thì

>>> from datetime import date
>>> d = date[2002, 12, 31]
>>> d.replace[day=26]
datetime.date[2002, 12, 26]
09 được thay thế cho năm 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
5 cho tháng và ngày

Đối với các đối tượng, không nên sử dụng mã định dạng cho giờ, phút, giây và micro giây vì các đối tượng không có các giá trị như vậy. Nếu chúng vẫn được sử 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]
14 được thay thế cho chúng

Vì lý do tương tự, việc xử lý các chuỗi định dạng chứa các điểm mã Unicode không thể được biểu thị trong bộ ký tự của ngôn ngữ hiện tại cũng phụ thuộc vào nền tảng. Trên một số nền tảng, các điểm mã như vậy được giữ nguyên trong đầu ra, trong khi trên các nền tảng khác, thay vào đó,

>>> 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 có thể tăng hoặc trả về một chuỗi trống

ghi chú

  1. Vì định dạng phụ thuộc vào ngôn ngữ hiện tại nên cần cẩn thận khi đưa ra các giả định về giá trị đầu ra. Thứ tự trường sẽ thay đổi [ví dụ: “tháng/ngày/năm” so với “ngày/tháng/năm”] và đầu ra có thể chứa các ký tự Unicode được mã hóa bằng cách sử dụng mã hóa mặc định của ngôn ngữ [ví dụ: nếu ngôn ngữ hiện tại là

    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    16,

  2. 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 có thể phân tích các năm trong phạm vi [1, 9999] đầy đủ, nhưng các năm < 1000 phải được điền bằng 0 để có chiều rộng 4 chữ số

    Đã thay đổi trong phiên bản 3. 2. Trong các phiên bản trước, 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]
    
    40 bị giới hạn trong các năm >= 1900.

    Đã thay đổi trong phiên bản 3. 3. Trong phiên bản 3. 2, 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]
    
    40 bị giới hạn trong các năm >= 1000.

  3. 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, lệnh
    >>> 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 chỉ ảnh hưởng đến trường giờ đầu ra nếu lệnh
    >>> 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 được sử dụng để phân tích cú pháp giờ

  4. Không giống như mô-đun, mô-đun không hỗ trợ giây nhảy vọt

  5. 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, chỉ thị
    >>> 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 chấp nhận từ một đến sáu chữ số và số 0 ở bên phải.
    >>> 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. Đối với một đối tượng ngây thơ, mã định 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]
    
    76 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]
    
    78 được thay thế bằng các chuỗi rỗng

    Đối với một đối tượng nhận thức

    >>> 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 được chuyển đổi thành một chuỗi có 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, trong đó
    >>> 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 là chuỗi 2 chữ số biểu thị số giờ bù giờ UTC,
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    38 là chuỗi 2 chữ số biểu thị số phút bù giờ UTC,
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    39 là chuỗi 2 chữ số biểu thị . Phần
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    40 bị bỏ qua khi phần bù là số nguyên của giây và cả phần
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    40 và phần
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    39 bị bỏ qua khi phần bù là số nguyên của phút. Ví dụ: nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta[seconds=57]
    >>> delta2 = timedelta[hours=25, seconds=2]
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57 trả về
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    45, thì
    >>> 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 được thay thế bằng chuỗi
    >>> 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.

    Đã thay đổi trong phiên bản 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. Ví dụ:
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    50 sẽ được phân tích thành phần bù của một giờ. Ngoài ra, việc cung cấp
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    51 giống hệt với
    >>> 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

    Trong

    >>> 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 được thay thế bằng một chuỗi rỗng nếu
    >>> 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 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 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 chỉ chấp nhận các giá trị nhất định cho
    >>> 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. bất kỳ giá trị nào trong

      >>> from datetime import date
      >>> d = date[2002, 12, 31]
      >>> d.replace[day=26]
      datetime.date[2002, 12, 26]
      
      61 cho ngôn ngữ máy của bạn

    2. các giá trị mã hóa cứng

      >>> 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

    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. Nó sẽ 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]
    
    93 cho các giá trị không hợp lệ

    Đã thay đổi trong phiên bản 3. 2. Khi chỉ thị

    >>> 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 được cung cấp cho phương thức
    >>> 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, một đối tượng nhận biết sẽ được tạo ra.
    >>> 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 của kết quả sẽ được đặt thành một thể hiện.

  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. Tương tự như

    >>> 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,
    >>> 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 chỉ được sử dụng trong tính toán khi ngày trong tuần và năm ISO [
    >>> 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] được chỉ định trong chuỗi định 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]
    
    41. Cũng lưu ý rằ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]
    
    87 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]
    
    69 không thể hoán đổi cho nhau

  9. 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, số 0 đứng đầu là tùy chọn đối với các định 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]
    
    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 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]
    
    88. Định 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]
    
    68 không yêu cầu số 0 đứng đầu

chú thích

Nghĩa là, nếu chúng ta bỏ qua các tác động của Thuyết tương đối

Điều này phù hợp với định nghĩa của lịch “proleptic Gregorian” trong cuốn sách Tính toán Lịch của Dershowitz và Reingold, trong đó nó là lịch cơ sở cho tất cả các phép tính. Xem sách để biết các thuật toán chuyển đổi giữa các thứ tự Gregorian proleptic và nhiều hệ thống lịch khác

Làm cách nào để in thời gian với múi giờ bằng Python?

Bạn có thể lấy thời gian hiện tại trong một múi giờ cụ thể bằng cách sử dụng mô-đun datetime với một mô-đun khác có tên là pytz. Sau đó, bạn có thể kiểm tra tất cả các múi giờ có sẵn bằng đoạn mã bên dưới. từ nhập ngày giờ nhập ngày giờ nhập vùng pytz = pytz. in all_timezones[khu vực] # Đầu ra. tất cả các múi giờ trên thế giới .

UTC trong Python là gì?

ngày giờ. múi giờ[offset, name=None] Ở đây offset thể hiện sự khác biệt giữa giờ địa phương và UTC [ Giờ phối hợp quốc tế ]. Nó có thể là một đối tượng đồng bằng thời gian nằm trong khoảng từ giờ=-24 đến +24.

Chủ Đề