Làm thế nào để tôi chỉ nhận được ngày trong python?

Tất cả chúng ta đều thường xuyên quan sát ngày và thời gian, đó là những thước đo quan trọng. Là lập trình viên, chúng ta thường xuyên phải loay hoay với ngày và giờ cho các tác vụ khác nhau như ghi lại ngày sinh và tính toán sự chênh lệch giữa các ngày tính theo ngày. Nhiều lần chúng tôi muốn truy cập vào ngày hiện tại và thực hiện một số thao tác trên đó để nhận được một số kết quả. Trong bài viết này, chúng ta sẽ tìm hiểu cách lấy năm hiện tại trong python

Mô-đun DateTime là gì?

Để hoàn thành công việc một cách nhanh chóng với cú pháp rõ ràng và đơn giản, Python có rất nhiều thư viện tích hợp sẵn bên cạnh vô số thư viện bên ngoài do các nhà phát triển tạo ra. Datetime là một mô-đun như vậy;

Datetime cung cấp cú pháp đơn giản và đơn giản hóa việc xử lý ngày tháng. Let's examine some potential methods for obtaining the current year with this module

How to Get Current Year in Python?

There are two methods to get the current year in Python. Let's look at them

1] By Using today[] method

We use the today[] method inside the date class to fetch today's date. This class is present inside the datetime module

For example

import datetime

today = datetime.date.today[]

print[today]

 

Output

2022-06-22

 

Now to just get the year from this returned date object we have two ways. Let's see them one by one

a] Accessing the year attribute

Utilizing the datetime module is the quickest way to determine the current year. We can directly access the year attribute of the returned object

For example

import datetime

today = datetime.date.today[]

year = today.year

print[year]

 

Output

2022

 

b] With srtftime[] function 

Hàm strfttime[] trong Python là một phương pháp khác để lấy năm hiện tại. Hàm strftime[] lấy một chuỗi chỉ định định dạng ngày làm đối số. Nó trả về ngày đã cho dưới dạng một chuỗi ở định dạng được cung cấp

Chúng tôi sẽ chuyển "% Y" tới strftime[] để lấy năm của đối tượng ngày tháng

For example

import datetime

today = datetime.date.today[]

year = today.strftime["%Y"]

print[year]

 

Output

2022

 

2] Bằng cách sử dụng phương thức now[]

Để có được ngày và giờ hiện tại, chúng ta cũng có thể sử dụng phương thức now[] có trong lớp datetime bên trong mô-đun datetime. Sau đó, để lấy năm hiện tại, chúng ta có thể truy cập thuộc tính năm hoặc sử dụng phương thức strftime[]

For example

import datetime

today = datetime.datetime.now[]

#accessing the year attribute
year1 = today.year
print[year1]

#using the strftime[] method
year2 = today.strftime["%Y"]
print[year2]

 

Output

2022
2022

 

Phần kết luận

Python với bộ sưu tập thư viện tích hợp khổng lồ biến một nhiệm vụ tẻ nhạt thành một công việc rất dễ dàng. Trong bài viết này, chúng ta đã học được hai phương pháp để lấy năm hiện tại trong python, có thể dễ dàng thực hiện với mô-đun datetime.  

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ể biểu thị 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ờ. MAXYEAR

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

New in version 3. 2

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

quan hệ phân lớp

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime

Tài sản chung

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

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

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

  • Objects of these types support efficient pickling via the module

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ơ

The distinction between aware and naive doesn’t apply to objects

Các đối tượng

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

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ị đó

  • A millisecond is converted to 1000 microseconds

  • A minute is converted to 60 seconds

  • An hour is converted to 3600 seconds

  • A week is converted to 7 days

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

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

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

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

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

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

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

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

Note that normalization of negative values may be surprising at first. For example

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

Class attributes

timedelta. min

The most negative object,

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

timedelta. max

The most positive object,

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

timedelta. resolution

The smallest possible difference between non-equal objects,

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

Note that, because of normalization,

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

Instance attributes [read-only]

Attribute

Value

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

Between -999999999 and 999999999 inclusive

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

Between 0 and 86399 inclusive

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

Between 0 and 999999 inclusive

Supported operations

Operation

Result

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

Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. [1]

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

Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. [1][6]

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

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

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

In general, t1 * i == t1 * [i-1] + t1 is true. [1]

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

Delta multiplied by a float. The result is rounded to the nearest multiple of timedelta. resolution using round-half-to-even

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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

Division [3] of overall duration t2 by interval unit t3. Returns a object

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

Delta divided by a float or an int. The result is rounded to the nearest multiple of timedelta. resolution using round-half-to-even

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 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]
41

The floor is computed and the remainder [if any] is thrown away. In the second case, an integer is returned. [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

The remainder is computed as a object. [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

Computes the quotient and the remainder.

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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] and
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 is an integer and r is a object

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

Returns a object with the same value. [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

equivalent to [-t1. days, -t1. seconds, -t1. microseconds], and to 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

equivalent to +t 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]
53, and to -t 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]
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

Returns a string in the form

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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, where D is negative for negative
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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

Returns a string representation of the object as a constructor call with canonical attribute values

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

________số 8_______

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

New in version 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]

All arguments are required. Arguments must be integers, in the following ranges

  • >>> from datetime import timedelta
    >>> delta = timedelta[
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=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

If an argument outside those ranges is given, is raised

Other constructors, all class methods

classmethod date. today[]

Return the current local date

This is equivalent to

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

classmethod date. fromtimestamp[timestamp]

Return the local date corresponding to the POSIX timestamp, such as is returned by

This may raise , if the timestamp is out of the range of values supported by the platform 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 function, and on
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 failure. It’s common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by

Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform 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 function. Raise instead of on
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 failure.

classmethod date. fromordinal[ordinal]

Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1

is raised unless

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

classmethod date. fromisoformat[date_string]

Return a corresponding to a date_string given in any valid ISO 8601 format, except ordinal dates [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]

New in version 3. 7

Changed in version 3. 11. Previously, this method only supported the format

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

classmethod date. fromisocalendar[year , week , day]

Return a corresponding to the ISO calendar date specified by year, week and day. This is the inverse of the function

New in version 3. 8

Class attributes

date. min

The earliest representable date,

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

date. max

The latest representable date,

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

date. resolution

The smallest possible difference between non-equal date objects,

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

Instance attributes [read-only]

date. year

Between and inclusive

date. month

Between 1 and 12 inclusive

date. day

Between 1 and the number of days in the given month of the given year

Supported operations

Operation

Result

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

date2 will be

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

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

Computes date2 such that

>>> 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 is moved forward in time if

    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    26, or backward if
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    27. Afterward
    >>> 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. is raised if
    >>> from datetime import timedelta
    >>> d = timedelta[microseconds=-1]
    >>> [d.days, d.seconds, d.microseconds]
    [-1, 86399, 999999]
    
    32 would be smaller than or larger than

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

  3. This is exact, and cannot overflow. timedelta. seconds and timedelta. microseconds are 0, and date2 + timedelta == date1 after

  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

In Boolean contexts, all objects are considered to be true

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

date. replace[year=self. year , month=self. month , day=self. day]

Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified

Thí dụ

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

date. timetuple[]

Return a such as returned by

The hours, minutes and seconds are 0, and the DST flag is -1

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

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
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ư. See also

date. isoweekday[]

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

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
59, a Wednesday. See also ,

date. isocalendar[]

Return a object 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 ISO calendar is a widely used variant of the Gregorian calendar.

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. The first week of an ISO year is the first [Gregorian] calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year

For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 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

Changed in version 3. 9. Result changed from a tuple to a .

date. isoformat[]

Return a string representing the date in ISO 8601 format,

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

date. __str__[]

For a date d,

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
66 is equivalent to
>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
67

date. ctime[]

Return a string representing the date

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 is equivalent to

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

on platforms where the native C

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
69 function [which invokes, but which does not invoke] conforms to the C standard

date. strftime[format]

Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. For a complete list of formatting directives, see

date. __format__[format]

Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see

Examples of Usage.

Example of counting days to an event

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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

More examples of working with

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

Các đối tượng

A object is a single object containing all the information from a object and a object

Like a object, assumes the current Gregorian calendar extended in both directions; like a object, assumes there are exactly 3600*24 seconds in every day

Constructor

class datetime. datetime[year , month , day , hour=0 , minute=0 , second=0 , microsecond=0 , tzinfo=None , * , fold=0]

The year, month and day arguments are required. tzinfo may be

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 an instance of a subclass. The remaining arguments must be integers in the following ranges

  • >>> from datetime import timedelta
    >>> delta = timedelta[
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=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

If an argument outside those ranges is given, is raised

New in version 3. 6. Added the

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

Other constructors, all class methods

classmethod datetime. today[]

Return the current local datetime, with

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

Equivalent to

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

See also ,

This method is functionally equivalent to , but without a

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

classmethod datetime. now[tz=None]

Return the current local date and time

If optional argument tz 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 or not specified, this is like , but, if possible, supplies more precision than can be gotten from going through a timestamp [for example, this may be possible on platforms supplying the C
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
06 function]

If tz is not

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03, it must be an instance of a subclass, and the current date and time are converted to tz’s time zone

This function is preferred over and

classmethod datetime. utcnow[]

Return the current UTC date and time, with

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

This is like , but returns the current UTC date and time, as a naive object. An aware current UTC datetime can be obtained by calling

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

Warning

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 such, the recommended way to create an object representing the current time in UTC is by calling
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
15

classmethod datetime. fromtimestamp[timestamp , tz=None]

Return the local date and time corresponding to the POSIX timestamp, such as is returned by . If optional argument tz 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 or not specified, the timestamp is converted to the platform’s local date and time, and the returned object is naive

If tz is not

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03, it must be an instance of a subclass, and the timestamp is converted to tz’s time zone

may raise , if the timestamp is out of the range of values supported by the platform 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 or
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 functions, and on
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 or
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 failure. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by , and then it’s possible to have two timestamps differing by a second that yield identical objects. This method is preferred over

Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform 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 or
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 functions. Raise instead of on
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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 or
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 failure.

Changed in version 3. 6. may return instances with set to 1.

classmethod datetime. utcfromtimestamp[timestamp]

Return the UTC corresponding to the POSIX timestamp, with

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03. [The resulting object is naive. ]

This may raise , if the timestamp is out of the range of values supported by the platform C

>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 function, and on
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 failure. It’s common for this to be restricted to years in 1970 through 2038

To get an aware object, call

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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

On the POSIX compliant platforms, it is equivalent to the following expression

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=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

except the latter formula always supports the full years range. between and inclusive

Warning

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 such, the recommended way to create an object representing a specific timestamp in UTC is by calling
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
58

Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C

>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 function. Raise instead of on
>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
28 failure.

classmethod datetime. fromordinal[ordinal]

Return the corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. is raised unless

>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
67. The hour, minute, second and microsecond of the result are all 0, and 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

classmethod datetime. combine[date , time , tzinfo=self. tzinfo]

Return a new object whose date components are equal to the given object’s, and whose time components are equal to the given object’s. If the tzinfo argument is provided, its value is used to set the attribute of the result, otherwise the attribute of the time argument is used

For any object d,

>>> timedelta[hours=-5]
datetime.timedelta[days=-1, seconds=68400]
>>> print[_]
-1 day, 19:00:00
76. If date is a object, its time components and attributes are ignored

Changed in version 3. 6. Added the tzinfo argument.

classmethod datetime. fromisoformat[date_string]

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

  1. Time zone offsets may have fractional seconds

  2. The

    >>> timedelta[hours=-5]
    datetime.timedelta[days=-1, seconds=68400]
    >>> print[_]
    -1 day, 19:00:00
    
    80 separator may be replaced by any single unicode character

  3. Ordinal dates are not currently supported

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

Examples

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

New in version 3. 7

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

classmethod datetime. fromisocalendar[year , week , day]

Return a corresponding to the ISO calendar date specified by year, week and day. The non-date components of the datetime are populated with their normal default values. This is the inverse of the function

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

Class attributes

datetime. min

The earliest representable ,

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

datetime. max

The latest representable ,

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

datetime. resolution

The smallest possible difference between non-equal objects,

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

Instance attributes [read-only]

datetime. year

Between and inclusive

datetime. month

Between 1 and 12 inclusive

datetime. day

Between 1 and the number of days in the given month of the given year

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. [Một khoảng thời gian lặp lại xảy ra khi đồng hồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] The value 0 [1] represents the earlier [later] of the two moments with the same wall time representation

New in version 3. 6

Supported operations

Operation

Result

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

[1]

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

[2]

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

[3]

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

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. được nâng lên nếu datetime2. year would be smaller than or larger than . Note that no time zone adjustments are done even if the input is an aware object

  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. replace[year=self. year , month=self. month , day=self. day , hour=self. hour , minute=self. minute , second=self. second , microsecond=self. microsecond , tzinfo=self. tzinfo , * , fold=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. 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 datetime from an aware datetime with no conversion of date and time data

New in version 3. 6. Added the

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

datetime. astimezone[tz=None]

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

If provided, tz must be an instance of a subclass, and its and methods must 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. 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

If called without arguments [or with

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
60] the system local timezone is assumed for the target timezone. Thuộc tính
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
61 của phiên bản datetime đã chuyển đổi sẽ được đặt thành phiên bản có tên vùng và phần bù thu được từ HĐH

Nếu

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
63 là tz, thì
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
64 là tự. không thực hiện điều chỉnh dữ liệu ngày hoặc giờ. Mặt khác, kết quả là giờ địa phương trong múi giờ tz, biểu thị cùng thời gian UTC với chính mình. after
>>> 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 will have the same date and time data as
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
67

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

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
68. 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. Ignoring error cases, acts like

>>> 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. The method can now be called on naive instances that are presumed to represent system local time.

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

ngày giờ. dst[]

Nếu là

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

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

ngày giờ. tzname[]

Nếu là

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

ngày giờ. thời gian[]

Return a such as returned by

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

>>> 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. 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 or 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,
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 is set to
>>> 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; else if returns a non-zero value,
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 is set 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
5; else
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 is set to
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
14

ngày giờ. utctimetuple[]

If instance d is naive, this is the same as

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
52 except that
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 is forced to 0 regardless of what
>>> 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 returns. DST không bao giờ có hiệu lực trong thời gian UTC

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

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

Warning

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

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. The method uses the attribute to disambiguate the times during a repeated interval.

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

Examples

>>> 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. Include full time, but truncate fractional second part to milliseconds.
    >>> 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. Include full time 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'
    
    80 format

Note

Excluded time components are truncated, not rounded

will be raised on an invalid timespec argument

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

New in version 3. 6. Added the timespec argument.

datetime. __str__[]

For a instance d,

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
66 is equivalent to
>>> 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

datetime. ctime[]

Return a string representing the date and time

>>> 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 is equivalent to

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

on platforms where the native C

>>> from datetime import timedelta
>>> d = timedelta[microseconds=-1]
>>> [d.days, d.seconds, d.microseconds]
[-1, 86399, 999999]
69 function [which invokes, but which does not invoke] conforms to the C standard

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]

Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see

Examples of Usage.

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

lớp ngày giờ. thời gian[giờ=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0]

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

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

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

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

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

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

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

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à

Class attributes

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ợ

Instance attributes [read-only]

thời gian. giờ

In

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

thời gian. phút

In

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

thời gian. giây

In

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

thời gian. micro giây

In

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

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

thời gian. gấp

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. [Một khoảng thời gian lặp lại xảy ra khi đồng hồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] 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. Nếu một so sánh là ngây thơ và người kia nhận thức được, sẽ được nâng lên nếu cố gắng so sánh thứ tự. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng các trường hợp nhận thức

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

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
30]. Để ngăn các so sánh kiểu hỗn hợp quay trở lại so sánh mặc định theo địa chỉ đối tượng, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi phép so sánh là
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
75 hoặc
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
76. Các trường hợp sau trả về hoặc , tương ứng

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

Trong ngữ cảnh Boolean, một đối tượng luôn được coi là đúng

Đã thay đổi trong phiên bản 3. 5. Trước Python 3. 5, một đối tượng được coi là sai nếu nó biểu thị nửa đêm theo giờ UTC. Hành vi này được coi là tối nghĩa và dễ bị lỗi và đã bị xóa trong Python 3. 5. Xem bpo-13936 để biết chi tiết đầy đủ.

nhà xây dựng khác

phương pháp phân lớp thời gian. fromisoformat[time_string]

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

  1. Time zone offsets may have fractional seconds

  2. >>> timedelta[hours=-5]
    datetime.timedelta[days=-1, seconds=68400]
    >>> print[_]
    -1 day, 19:00:00
    
    80 ở đầu, thường được yêu cầu trong trường hợp có thể có sự không rõ ràng giữa ngày và giờ, không bắt buộc

  3. Phân số giây có thể có bất kỳ số lượng chữ số nào [bất kỳ số nào vượt quá 6 sẽ bị cắt ngắn]

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

Examples

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

New in version 3. 7

Đã thay đổi trong phiên bản 3. 11. Previously, this method only supported formats that could be emitted by .

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

thời gian. thay thế[giờ=chính mình. giờ , phút=chính mình. phút , giây=chính mình. second , microsecond=self. micro giây , tzinfo=self. tzinfo , * , gấp=0]

Return a with the same value, except for those attributes given new values by whichever keyword arguments are specified. 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 ngây thơ từ một nhận thức mà không cần chuyển đổi dữ liệu thời gian

New in version 3. 6. Added the

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

time. isoformat[timespec=']

Trả về một chuỗi biểu thị thời gian ở định dạng ISO 8601, một 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'
    
    80, nếu không phải là 0

  • >>> delta2 > delta1
    True
    >>> delta2 > 5
    Traceback [most recent call last]:
      File "", line 1, in 
    TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
    
    76, nếu là 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, nếu 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

  • >>> # 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, nếu là 0 và 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

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. Include full time, but truncate fractional second part to milliseconds.
    >>> 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. Include full time 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'
    
    80 format

Note

Excluded time components are truncated, not rounded

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

Thí dụ

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

New in version 3. 6. Added the timespec argument.

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

thời gian. thời gian chạy[định dạng]

Trả về một chuỗi đại diện cho thời gian, được kiểm soát bởi một chuỗi định dạng rõ ràng. For a complete list of formatting directives, see

thời gian. __format__[định dạng]

Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see

thời gian. 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, ngược lại trả về
>>> # Components of another_year add up to exactly 365 days
>>> from datetime import timedelta
>>> year = timedelta[days=365]
>>> another_year = timedelta[weeks=40, days=84, hours=23,
..                          minutes=50, seconds=600]
>>> year == another_year
True
>>> year.total_seconds[]
31536000.0
90 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.

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

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

thời gian. tzname[]

Nếu là

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03, trả về
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03, ngược lại trả về
>>> from datetime import timedelta
>>> 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 hoặc đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03 hoặc một đối tượng chuỗi

Examples of Usage.

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ể

Một thể hiện của [một lớp con cụ thể của] có thể được truyền cho các hàm tạo cho và các đối tượng. 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ỹ

Yêu cầu đặc biệt đối với dưa chua. 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. Đây là yêu cầu kỹ thuật có thể được nới lỏng trong tương lai

Một lớp con cụ thể của có thể cần phải thực hiện các phương pháp sau. 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. Nếu giờ địa phương ở phía tây của UTC, điều này sẽ là âm

Điều này thể hiện tổng phần bù từ UTC; . Nếu không biết phần bù UTC, hãy trả về

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03. Mặt khác, giá trị được trả về phải là một đối tượng nằm trong khoảng từ
>>> from datetime import timedelta
>>> year = timedelta[days=365]
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta[days=3650]
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta[days=3285]
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
[datetime.timedelta[days=1095], 3]
26 đế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]
27 [độ lớn của phần bù phải nhỏ hơn một ngày]. Hầu hết các triển khai có thể sẽ giống như một trong hai

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

Nếu 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, thì cũng không nên 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

Việc thực hiện tăng mặc định

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

Trả lại điều chỉnh thời gian tiết kiệm ánh sáng ban ngày [DST], dưới dạng đối tượng hoặc

>>> from datetime import timedelta
>>> delta = timedelta[
..     days=50,
..     seconds=27,
..     microseconds=10,
..     milliseconds=29000,
..     minutes=5,
..     hours=8,
..     weeks=2
.. ]
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta[days=64, seconds=29156, microseconds=10]
03 nếu không biết thông tin DST

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]
83 nếu DST không có hiệu lực. Nếu DST có hiệu lực, hãy trả lại phần bù dưới dạng đối tượng [xem để biết chi tiết]. Lưu ý rằng phần bù DST, nếu có, đã được thêm vào phần bù UTC được trả về bởi , do đó, không cần tham khảo trừ khi bạn muốn nhận thông tin DST riêng. Ví dụ: gọi phương thức thuộc tính của nó để xác định cách đặt cờ
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 và gọi để giải thích cho các thay đổi DST khi vượt qua các múi giờ

Một thể hiện tz của một lớp con mô hình hóa cả thời gian tiêu chuẩn và thời gian ban ngày phải nhất quán theo nghĩa này

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

phải trả về cùng một kết quả cho mọi dt với

>>> 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 Đối với các lớp con lành mạnh, biểu thức này mang lại "độ lệch chuẩn" của múi giờ, không phụ thuộc vào ngày hoặc giờ mà chỉ phụ thuộc vào vị trí địa lý. Việc thực hiện ỷ lại vào việc này nhưng không phát hiện được vi phạm; . Nếu một lớp con không thể đảm bảo điều này, nó có thể ghi đè cài đặt mặc định của để hoạt động chính xác với
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 bất kể

Hầu hết các triển khai có thể sẽ giống như một trong hai

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

hoặc là

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

Việc thực hiện tăng mặc định

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

tzinfo. tzname[dt]

Trả về tên múi giờ tương ứng với đối tượng dt, dưới dạng chuỗi. Không có gì về tên chuỗi được xác định bởi mô-đun và không có yêu cầu rằng nó có ý nghĩa cụ thể. Ví dụ: “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” đều là những câu trả lời hợp lệ. 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 biết tên chuỗi. Lưu ý rằng đây chủ yếu là một phương thức chứ không phải là một chuỗi cố định vì một số lớp con sẽ muốn trả về các tên khác nhau tùy thuộc vào giá trị cụ thể của dt được truyền, đặc biệt nếu lớp đó đang tính thời gian ban ngày

Việc thực hiện tăng mặc định

Các phương thức này được gọi bởi một hoặc đối tượng, để đáp lại các phương thức cùng tên của chúng. Một đối tượng chuyển chính nó làm đối số và một đối tượng chuyể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]
03 làm đối số. Do đó, các phương thức của lớp con nên được chuẩn bị để chấp nhận đối số dt 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]
03 hoặc của lớp

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]
03 được thông qua, người thiết kế lớp sẽ quyết định câu trả lời tốt nhất. Ví dụ: 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 là phù hợp nếu lớp muốn nói rằng các đối tượng thời gian không tham gia vào các giao thức. Có thể hữu ích hơn khi
>>> 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 trả về phần bù UTC tiêu chuẩn, vì không có quy ước nào khác để khám phá phần bù tiêu chuẩn

Khi một đối tượng được truyền để phản hồi một phương thức, thì

>>> 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 chính là đối tượng như self. các phương thức có thể dựa vào điều này, trừ khi mã người dùng gọi trực tiếp các phương thức. Mục đích là các phương thức diễn giải dt theo giờ địa phương và không cần lo lắng về các đối tượng trong các múi giờ khác

Có một phương thức nữa mà một lớp con có thể muốn ghi đè

tzinfo. từutc[dt]

Điều này được gọi từ việc thực hiện mặc định. Khi được gọi từ đó,

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

Hầu hết các lớp con sẽ có thể kế thừa việc triển khai mặc định mà không gặp sự cố. Nó đủ mạnh để xử lý các múi giờ có độ lệch cố định và các múi giờ chiếm cả thời gian tiêu chuẩn và thời gian ban ngày và cả thời gian sau ngay cả khi thời gian chuyển đổi DST khác nhau trong các năm khác nhau. Một ví dụ về múi giờ mà việc triển khai mặc định có thể không xử lý chính xác trong mọi trường hợp là trường hợp mà độ lệch tiêu chuẩn [từ UTC] phụ thuộc vào ngày và giờ cụ thể đã trôi qua, điều này có thể xảy ra vì lý do chính trị. Việc triển khai mặc định của

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

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

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

Trong tệp

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

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

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

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

Khi DST bắt đầu [dòng “bắt đầu”], đồng hồ treo tường địa phương nhảy từ 1. 59 đến 3. 00. Một bức tường thời gian của mẫu 2. MM không thực sự có ý nghĩa vào ngày đó, vì vậy

>>> 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 sẽ không mang lại kết quả với
>>> 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 vào ngày DST bắt đầu. Ví dụ: tại chuyển tiếp mùa xuân năm 2016, chúng tôi nhận được

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

Khi DST kết thúc [dòng “kết thúc”], có khả năng xảy ra sự cố tồi tệ hơn. có một giờ không thể đánh vần rõ ràng theo giờ địa phương. giờ cuối cùng của thời gian ban ngày. Ở phương Đông, đó là thời gian của mẫu 5. MM UTC vào ngày thời gian ban ngày kết thúc. Đồng hồ treo tường địa phương nhảy từ 1. 59 [thời gian ban ngày] trở lại 1. 00 [giờ chuẩn] lại. Giờ địa phương của mẫu 1. MM không rõ ràng.

>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
72 bắt chước hành vi của đồng hồ địa phương bằng cách ánh xạ hai giờ UTC liền kề vào cùng một giờ địa phương sau đó. Trong ví dụ phương Đông, thời gian UTC có dạng 5. MM và 6. MM cả ánh xạ tới 1. MM khi được chuyển đổi sang phương Đông, nhưng các lần trước thuộc tính được đặt thành 0 và các lần sau thuộc tính được đặt thành 1. Ví dụ: tại Fall back transition năm 2016, chúng tôi nhận được

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

Lưu ý rằng các trường hợp chỉ khác nhau về giá trị của thuộc tính được coi là bằng nhau khi so sánh

Các ứng dụng không thể chịu được sự mơ hồ về thời gian nên kiểm tra rõ ràng giá trị của thuộc tính hoặc tránh sử dụng các lớp con kết hợp;

Xem thêm

Mô-đun có một lớp cơ bản [để xử lý các giá trị bù trừ cố định tùy ý từ UTC] và thuộc tính của nó [một thể hiện múi giờ UTC]

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback [most recent call last]:
  File "", line 1, in 
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
1 mang cơ sở dữ liệu múi giờ IANA [còn được gọi là cơ sở dữ liệu Olson] cho Python và việc sử dụng cơ sở dữ liệu này được khuyến nghị

Cơ sở dữ liệu múi giờ IANA

Cơ sở dữ liệu múi giờ [thường được gọi là tz, tzdata hoặc zoneinfo] chứa mã và dữ liệu biểu thị lịch sử giờ địa phương cho nhiều vị trí đại diện trên toàn cầu. Nó được cập nhật định kỳ để phản ánh những thay đổi do các cơ quan chính trị thực hiện đối với ranh giới múi giờ, độ lệch UTC và quy tắc tiết kiệm ánh sáng ban ngày

Các đối tượng

Lớp này là một lớp con của , mỗi phiên bản đại diện cho một múi giờ được xác định bởi một phần bù cố định từ UTC

Các đối tượng của lớp này không thể được sử dụng để biểu thị thông tin múi giờ ở những vị trí sử dụng các độ lệch khác nhau trong các ngày khác nhau trong năm hoặc khi các thay đổi lịch sử đã được thực hiện đối với thời gian dân sự

lớp ngày giờ. múi giờ[độ lệch , tên=None]

Đối số offset phải được chỉ định làm đối tượng biểu thị sự khác biệt giữa giờ địa phương và UTC. Nó phải hoàn toàn nằm trong khoảng từ

>>> from datetime import timedelta
>>> year = timedelta[days=365]
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta[days=3650]
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta[days=3285]
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
[datetime.timedelta[days=1095], 3]
26 đế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]
27, nếu không sẽ được nâng lên

Đối số tên là tùy chọn. Nếu được chỉ định, nó phải là một chuỗi sẽ được sử dụng làm giá trị được trả về bởi phương thức

New in version 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]

Trả về giá trị cố định được chỉ định khi phiên bản được tạo

Đối số dt bị bỏ qua. Giá trị trả về là một ví dụ bằng với sự khác biệt giữa giờ địa phương và 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]

Trả về giá trị cố định được chỉ định khi phiên bản được tạo

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 offset 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, thì tên là “UTC”, nếu không thì 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 biết, với _____8_______4 được đặt thành _____83_______38

Class attributes

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 một chuỗi biểu thị thời gian dưới sự kiểm soát của một 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 cú pháp một chuỗi thành một đối tượng có định dạng tương ứng

Loại phương pháp

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

Thí dụ

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

Năm không có thế kỷ dưới dạng số thập phân không đệm

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

Phút dưới dạng số thập phân không đệm

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

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

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

Số tuần của năm [Thứ Hai 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 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_Mỹ];

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. These parameters all correspond to ISO 8601 date values

chỉ thị

Nghĩa

Thí dụ

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

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

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

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

[8]

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

ISO 8601 weekday as a decimal number where 1 is Monday

1, 2, …, 7

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

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

01, 02, …, 53

[8], [9]

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

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

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

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

New in version 3. 6.

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

Technical Detail

Broadly speaking,

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

For the class method, the default value is

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

Using

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

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

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

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

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

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

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

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

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

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

ghi chú

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

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

  2. The

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

    Changed in version 3. 2. In previous versions,

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

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

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

  3. When used with the

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

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

  5. When used with the

    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    41 method, the
    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    75 directive accepts from one to six digits and zero pads on the right.
    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    75 is an extension to the set of format characters in the C standard [but implemented separately in datetime objects, and therefore always available]

  6. For a naive object, the

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

    For an aware object

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

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

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

    Changed in version 3. 7. When the

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

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

    In

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

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

    1. any value in

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

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

    So someone living in Japan may have

    >>> 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, and
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    63 as valid values, but probably not
    >>> from datetime import date
    >>> d = date[2002, 12, 31]
    >>> d.replace[day=26]
    datetime.date[2002, 12, 26]
    
    67. It will raise
    >>> from datetime import timedelta
    >>> delta = timedelta[
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. ]
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta[days=64, seconds=29156, microseconds=10]
    
    93 for invalid values

    Changed in version 3. 2. When the

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

  7. When used with the

    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    41 method,
    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    80 and
    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    81 are only used in calculations when the day of the week and the calendar year [
    >>> from datetime import date
    >>> date.fromisoformat['2019-12-04']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['20191204']
    datetime.date[2019, 12, 4]
    >>> date.fromisoformat['2021-W01-1']
    datetime.date[2021, 1, 4]
    
    69] are specified

  8. Similar to

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

  9. When used with the

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

Footnotes

If, that is, we ignore the effects of Relativity

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

How to get date only from timestamp in Python?

The fromtimestamp[] function is used to return the date associated with a given timestamp

How to extract date from string Python?

To extract a date from a string, you must first understand the format of the date. Để trích xuất ngày, chỉ cần sử dụng biểu thức chính quy và "datetime. ngày giờ. strptime" để phân tích cú pháp .

Làm cách nào để chỉ lấy ngày từ datetime trong gấu trúc Python?

Phương pháp 1. Sử dụng hàm ngày tháng .
dataframe là khung dữ liệu đầu vào
to_datetime là hàm dùng để chuyển chuỗi datetime thành datetime
DateTime là cột ngày giờ trong khung dữ liệu
đt. ngày được sử dụng để chuyển đổi ngày giờ thành ngày
Cột ngày là cột mới để lấy ngày từ datetime

Chủ Đề