Hướng dẫn date and time function in python - hàm ngày và giờ trong python

Trong bài viết này, bạn sẽ học cách có được ngày và giờ hiện tại ngày hôm nay ở Python. Chúng tôi cũng sẽ định dạng ngày và giờ ở các định dạng khác nhau bằng phương thức strftime ().

Show

Video: Ngày và giờ ở Python

Có một số cách bạn có thể thực hiện để có được ngày hiện tại. Chúng tôi sẽ sử dụng lớp

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
2 của mô -đun DateTime để hoàn thành nhiệm vụ này.


Ví dụ 1: Python Nhận ngày hôm nay

from datetime import date

today = date.today()
print("Today's date:", today)

Ở đây, chúng tôi đã nhập lớp

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
2 từ mô -đun
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
4. Sau đó, chúng tôi đã sử dụng phương pháp
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
5 để có được ngày địa phương hiện tại.

Nhân tiện,

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
5 trả về một đối tượng
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
2, được gán cho biến ngày nay trong chương trình trên. Bây giờ, bạn có thể sử dụng phương thức strftime () để tạo một chuỗi đại diện ở các định dạng khác nhau.


Ví dụ 2: Ngày hiện tại ở các định dạng khác nhau

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)

Khi bạn chạy chương trình, đầu ra sẽ giống như:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019

Nếu bạn cần có được ngày và giờ hiện tại, bạn có thể sử dụng lớp

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
4 của mô -đun
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
4.

Ví dụ 3: Nhận ngày và giờ hiện tại

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	

Bạn sẽ cổng đầu ra như dưới đây.

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56

Ở đây, chúng tôi đã sử dụng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
0 để có được ngày và giờ hiện tại. Sau đó, chúng tôi đã sử dụng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
1 để tạo một chuỗi biểu diễn ngày và thời gian ở định dạng khác.

Mã nguồn: lib/datetime.py Lib/datetime.py


Mô -đun

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 cung cấp các lớp để thao tác ngày và thời gian.

Trong khi số học ngày và thời gian được hỗ trợ, trọng tâm của việc thực hiện là trích xuất thuộc tính hiệu quả cho định dạng đầu ra và thao tác.

Xem thêm

Mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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

Lịch chung các chức năng liên quan.

Mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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

Truy cập thời gian và chuyển đổi.

Mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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

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

Gói ngày

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.

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

Các đối tượng ngày và thời gian có thể được phân loại là nhận thức của người Hồi giáo hay người 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 thời gian tiết kiệm ánh sáng ban ngày, một đối tượng nhận thức có thể tự định vị so với các đối tượng nhận thức 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 mở để giải thích. 1aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation. 1

Một đối tượng ngây thơ không chứa đủ thông tin để định vị rõ ràng chính nó so với các đối tượng ngày/giờ khác. Cho dù một đối tượng ngây thơ đại diện cho thời gian phối hợp (UTC), thời gian địa phương hoặc thời gian trong một số múi giờ khác hoàn toàn tùy thuộc vào chương trình, giống như nó tùy thuộc vào chương trình cho dù một số cụ thể đại diện cho mét, dặm hoặc khối lượng. Các đối tượng ngây thơ rất dễ hiểu và làm việc cùng, với chi phí bỏ qua một số khía cạnh của thực tế.naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

Đối với các ứng dụng yêu cầu các đối tượng nhận thức, các đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 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)
4 có thuộc tính thông tin múi giờ tùy chọn,
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8, có thể được đặt thành một thể hiện của một lớp con của lớp Tóm tắ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)
8. Các đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8 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ể

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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, lớp
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2, được cung cấp bởi mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Lớp
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 có thể đại diện cho thời gian đơn giản với các độ lệch cố định từ UTC, chẳng hạn như chính UTC hoặc thời gian EST và EDT Bắc Mỹ. Hỗ trợ thời gian ở 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 có chính trị hơn là hợp lý, thay đổi thường xuyên và không có tiêu chuẩn phù hợp cho mọi ứng dụng ngoài UTC.

Hằng số trong

Mô -đun

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 xuất các hằng số sau:

datetime.minyear¶MINYEAR

Số năm nhỏ nhất cho phép trong một đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 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)
2.
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8 là
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9.

DateTime.Maxyear¶MAXYEAR

Số năm lớn nhất cho phép trong một đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 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)
2.
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2 là
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
3.

DateTime.utc¶UTC

Bí danh cho utc timezone singleton

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

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

Các loại có sẵn

classdatetime.datedatetime.date

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

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

classdatetime.time datetime.time

Một thời gian lý tưởng hóa, độc lập với 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. .

classdatetime.datetimedatetime.datetime

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

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5,
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
6,
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
7,
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
8,
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
9,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
0,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
1 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)
8.

classdatetime.timedeltadatetime.timedelta

Một thời gian thể hiện sự khác biệt giữa hai trường hợp

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
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)
4 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)
2 với độ phân giải micro giây.

classdatetime.tzinfodatetime.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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 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)
4 để cung cấp một khái niệm có thể tùy chỉnh về điều chỉnh thời gian (ví dụ, để tính đến múi giờ và/hoặc thời gian tiết kiệm ánh sáng ban ngày).

classdatetime.timezone datetime.timezone

Một lớp thực hiện lớp cơ sở trừu tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8 như một phần bù cố định từ UTC.

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

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

Mối quan hệ phân lớp:

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime

Tài sản chung¶

Các loại

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
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)
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)
4 và
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 chia sẻ các tính năng phổ biến này:

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

  • Mối quan hệ phân lớp:

  • Tài sản chung¶

Các loại >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) 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) 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) 4 và >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) 2 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 có thể băm, có nghĩa là chúng có thể được sử dụng làm khóa từ điển.

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
11.

Xác định xem một đối tượng được biết hay ngây thơ

  1. Đối tượng của loại

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    6 luôn ngây thơ.

  2. Một đối tượng của loại

    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=10,
    ...     milliseconds=29000,
    ...     minutes=5,
    ...     hours=8,
    ...     weeks=2
    ... )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    4 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)
    
    2 có thể nhận thức được hoặc ngây thơ.

Đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 D biết nếu cả hai điều sau đây giữ:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
16 không phải là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17

  1. from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    18 không trả lại
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    17

  2. Nếu không, D là ngây thơ.

Đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 t nhận thức được nếu cả hai điều sau đây giữ:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
21 không phải là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17

from datetime import date today = date.today() # dd/mm/YY d1 = today.strftime("%d/%m/%Y") print("d1 =", d1) # Textual month, day and year d2 = today.strftime("%B %d, %Y") print("d2 =", d2) # mm/dd/y d3 = today.strftime("%m/%d/%y") print("d3 =", d3) # Month abbreviation, day and year d4 = today.strftime("%b-%d-%Y") print("d4 =", d4) 23 không trả lại from datetime import date today = date.today() # dd/mm/YY d1 = today.strftime("%d/%m/%Y") print("d1 =", d1) # Textual month, day and year d2 = today.strftime("%B %d, %Y") print("d2 =", d2) # mm/dd/y d3 = today.strftime("%m/%d/%y") print("d3 =", d3) # Month abbreviation, day and year d4 = today.strftime("%b-%d-%Y") print("d4 =", d4) 17.

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

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25.
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 Đối tượng

Một đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 biểu thị thời lượng, sự khác biệt giữa hai ngày hoặc thời gian.

  • classDateTime.timedelta (ngày = 0, giây = 0, micro giây = 0, miliseconds = 0, phút = 0, giờ = 0, tuần = 0) ¶

  • Tất cả các đối số là tùy chọn và mặc định là

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    28. Đối số có thể là số nguyên hoặc phao, và có thể là tích cực hoặc tiêu cực.

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

  • Một mili giây được chuyển đổi thành 1000 micro giây.

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

  • from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    29

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

  • from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    31

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

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

và ngày, giây và micro giây sau đó được chuẩn hóa để biểu diễn là duy nhất, với

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 sẽ được nâng lên.

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

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

Thuộc tính lớp:

Timedelta.Min¶min

Đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 tiêu cực nhất,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
34.

Timedelta.Max¶max

Đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 tích cực nhất,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
36.

Timedelta.Resolution¶resolution

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 không bình đẳng,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
38.

Lưu ý rằng, vì bình thường hóa,

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
39>
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
40.
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
41 không thể đại diện cho một đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25.

Thuộc tính thể hiện (chỉ đọc):

Thuộc tính

Giá trị

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
43

Từ -999999999 đến 999999999

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
44

Từ 0 đến 86399 bao gồm

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
45

Từ 0 đến 999999 bao gồm

Hoạt động được hỗ trợ:

Hoạt động

Kết quả

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
46

Tổng của T2 và T3. Sau đó T1-T2 == T3 và T1-T3 == T2 là đúng. (1)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
47

Sự khác biệt của T2 và T3. Sau đó T1 == T2 - T3 và T2 == T1 + T3 là đúng. (1) (6)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
48

Delta nhân với một số nguyên. Sau đó T1 // I == T2 là đúng, được cung cấp

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
49.

Nói chung, T1 * I == T1 * (I-1) + T1 là đúng. (1)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
50

Delta nhân với một chiếc phao. Kết quả được làm tròn đến bội số gần nhất của thời gian.Resolution bằng cách sử dụng một nửa vòng tròn.

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
51

Phân chia (3) của tổng số thời gian T2 theo đơn vị khoảng T3. Trả về một đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
52.

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
53

Delta chia cho một chiếc phao hoặc một int. Kết quả được làm tròn đến bội số gần nhất của thời gian.Resolution bằng cách sử dụng một nửa vòng tròn.

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
54 hoặc
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
55

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
56

Phần còn lại được tính là đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25. (3)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
58

Tính toán thương số và phần còn lại:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
59 (3) và
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
60. Q là một số nguyên và R là đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25.

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
62

Trả về một đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 có cùng giá trị. (2)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
64

tương đương với ________ 125 (-t1.days, -t1.seconds, -t1.MicroSeconds) và T1* -1. (1) (4)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
66

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
67 và đến -t khi
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
68. (2)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
69

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
70, trong đó D âm cho âm
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
71. (5)

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
72

Trả về một biểu diễn chuỗi của đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 dưới dạng lệnh gọi hàm tạo với các giá trị thuộc tính chính tắc.

Notes:

  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. Phân chia 0 tăng

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    74.

  4. -Timedelta.max không thể đại diện như một đối tượng

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    25.

  5. Các biểu diễn chuỗi của các đối tượng

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    25 đượ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 thời gian âm tính. 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 date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    77 sẽ luôn bằng biểu thức
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    78 trừ khi T3 bằng
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    39; Trong trường hợp đó, trước đây sẽ tạo ra một kết quả trong khi cái sau sẽ tràn.

Ngoài các hoạt động được liệt kê ở trên, các đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 hỗ trợ các bổ sung và phép trừ nhất định với các đối tượng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 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)
2 (xem bên dưới).

Đã thay đổi trong phiên bản 3.2: phân chia sàn và phân chia thực sự của đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 bởi một đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 khác hiện được hỗ trợ, như các hoạt động còn lại và hàm
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
85. Sự phân chia thực và phép nhân của đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 bởi một đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
52 hiện được hỗ trợ.Floor division and true division of a
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 object by another
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 object are now supported, as are remainder operations and the
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
85 function. True division and multiplication of a
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 object by a
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
52 object are now supported.

So sánh các đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 được hỗ trợ, với một số cảnh báo.

Các so sánh

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
89 hoặc
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
90 luôn trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
91, bất kể loại đối tượng 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 date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
92 và
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
93), khi một đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 được so sánh với một đối tượng thuộc loại khác,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
95 được nâng lên:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
0

Trong bối cảnh Boolean, một đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 được coi là đúng khi và chỉ khi nó không bằng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
97.

Phương pháp ví dụ:

Timedelta.total_seconds ()total_seconds()

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
98. Đối với các đơn vị khoảng khác ngoài giây, hãy sử dụng biểu mẫu phân chia trực tiếp (ví dụ:
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
99).

Lưu ý rằng trong các khoảng thời gian rất lớn (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 của micro.

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

Ví dụ về việc sử dụng: ________ 125¶

Một ví dụ bổ sung về bình thường hóa:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
1

Ví dụ về số học

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
2

>>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) 6 Đối tượng

Một đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 đại diện cho một ngày (năm, tháng và ngày) trong một lịch lý tưởng hóa, lịch hiện tại của Gregorian được mở rộng vô thời hạn theo cả hai hướng.

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

classdatetime.date (năm, tháng, ngày) ¶datetime.date(year, month, day)

Tất cả các lập luận được yêu cầu. Các đối số phải là số nguyên, trong các phạm vi sau:

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    04

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    05

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    06

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 sẽ được nêu ra.

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

classmethoddate.today ()date.today()

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

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
08.

ClassMethodDate.FromTimestamp (Timestamp) ¶date.fromtimestamp(timestamp)

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
09.

Điều này có thể tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32, nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi hàm C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 trên lỗi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11. Nó phổ biến vì điều này được giới hạn trong nhiều năm từ năm 1970 đến năm 2038. Lưu ý rằng trên các hệ thống không phải là posix bao gồm các giây nhảy vọt trong khái niệm dấu thời gian của họ, bước nhảy vọt bị bỏ qua bởi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14.

Đã thay đổi trong phiên bản 3.3: Tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi hàm C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11. Tăng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 khi thất bại
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11.Raise
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 if the timestamp is out of the range of values supported by the platform C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 function. Raise
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 on
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 failure.

classmethoddate.fromordinal (thứ tự) ¶ date.fromordinal(ordinal)

Trả lại ngày tương ứng với Ordinal Gregorian Progorian, trong đó ngày 1 tháng 1 năm 1 có thứ tự 1.

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 được nâng lên trừ khi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
22. Đối với bất kỳ ngày D,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
23.

classmethoddate.fromisoformat (date_string) ¶ date.fromisoformat(date_string)

Trả về

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 tương ứng với DATE_STRING được đưa ra ở bất kỳ định dạng ISO 8601 hợp lệ nào, ngoại trừ ngày thứ tự (ví dụ:
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
25):

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
3

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

Đã thay đổi trong phiên bản 3.11: Trước đây, phương pháp này chỉ hỗ trợ định dạng

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
26.Previously, this method only supported the format
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
26.

classmethoddate.fromisocalendar (năm, tuần, ngày) ¶date.fromisocalendar(year, week, day)

Trả về

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 tương ứng với ngày lịch ISO được chỉ định theo năm, tuần và ngày. Đây là nghịch đảo của hàm
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
28.

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

Thuộc tính lớp:

ngày.min¶min

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
29.

ngày.Max¶max

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
30.

ngày.Resolution¶resolution

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
31.

Thuộc tính thể hiện (chỉ đọc):

ngày.year¶year

Từ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8 đến
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2 bao gồm.

ngày.month

Từ 1 đến 12 bao gồm.

ngày ngày¶day

Từ 1 đến số ngày trong tháng nhất định của năm nhất định.

Hoạt động được hỗ trợ:

Hoạt động

Kết quả

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
34

Ngày 2 sẽ là

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
35 ngày sau ngày 1. (1)

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
36

Tính toán ngày2 sao cho

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
37. (2)

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
38

(3)

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
39

Ngày 1 được coi là nhỏ hơn ngày2 khi ngày 1 đi trước ngày 2 trong thời gian. (4)

Notes:

  1. Ngày 2 được chuyển tiếp theo thời gian nếu

    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    40 hoặc lùi nếu
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    41. Sau đó
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    42.
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    43 và
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    44 bị bỏ qua.
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    32 được tăng lên nếu
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    46 sẽ nhỏ hơn
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    8 hoặc lớn hơn
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    2.

  2. d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    43 và
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    44 bị bỏ qua.

  3. Điều này là chính xác, và không thể tràn. Timedelta.seconds và Timedelta.MicroSeconds là 0 và Date2 + TimedelTa == Date1 sau.

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

    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    39 khi và chỉ khi
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    52. So sánh ngày tăng
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 Nếu so sánh khác cũng không phải là đối tượng
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    6. Tuy nhiên,
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    55 được trả về thay vào đó nếu so sánh khác có thuộc tính
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    56. Móc này cung cấp cho các loại đối tượng ngày khác cơ hội thực hiện so sánh loại hỗn hợp. Nếu không, khi một đối tượng
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    6 được so sánh với một đối tượng thuộc loại khác,
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 được nâng lên trừ khi so sánh là
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    89 hoặc
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    90. Các trường hợp sau trở lại lần lượt là
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    61 hoặc
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    62.

Trong bối cảnh Boolean, tất cả các đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 được coi là đúng.

Phương pháp ví dụ:

date.replace (năm = self.year, tháng = self.month, day = self.day) ¶replace(year=self.year, month=self.month, day=self.day)

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

Example:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
4

ngày.TimeTuple () ¶timetuple()

Trả về một

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
64 như được trả về bởi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
65.

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
66 tương đương với:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
5

trong đó

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
67 là số ngày trong năm hiện tại bắt đầu với
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9 cho ngày 1 tháng 1.

ngày.toordinal () ¶toordinal()

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

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 D,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
23.

ngày.weekday () ¶weekday()

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
71, một ngày thứ Tư. Xem thêm
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
72.

ngày.isoweekday () ¶isoweekday()

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
73, thứ tư. Xem thêm
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
74,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
75.

ngày.isocalendar () ¶isocalendar()

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

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
77 và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
78.named tuple object with three components:
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
77 and
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
78.

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

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

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
6

Đã thay đổi trong phiên bản 3.9: Kết quả đã thay đổi từ một tuple thành một tuple có tên.Result changed from a tuple to a named tuple.

ngày.isoformat () ¶isoformat()

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
26:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
7

ngày .__ str __ ()__str__()

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
80 tương đương với
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
81.

ngày.ctime () ¶ctime()

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
8

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
82 tương đương với:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
9

Trên các nền tảng nơi hàm C

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
83 gốc (
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
84 gọi, nhưng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
85 không gọi) phù hợp với tiêu chuẩn C.

ngày.strftime (định dạng) ¶strftime(format)

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

ngày .__ Định dạng __ (Định dạng) ¶__format__(format)

Giống như

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
86. Điều này cho phép chỉ định một chuỗi định dạng cho một đối tượng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 trong các chuỗi chuỗi được định dạng và khi sử dụng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem hành vi strftime () và strptime ().formatted string literals and when using
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. For a complete list of formatting directives, see strftime() and strptime() Behavior.

Ví dụ về việc sử dụng: ________ 76¶

Ví dụ về việc đếm ngày cho một sự kiện:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
0

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

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
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) 2 Đối tượng

Đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 là một đối tượng duy nhất chứa tất cả thông tin từ đối tượng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 và đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4.

Giống như một đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
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)
2 giả định lịch Gregorian hiện tại được mở rộng theo cả hai hướng; Giống như một đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
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)
2 giả định có chính xác là 3600*24 giây mỗi ngày.

Constructor:

classDateTime.datetime (năm, tháng, ngày, giờ = 0, phút = 0, thứ hai = 0, microsecond = 0, tzinfo = none, *, gấp = 0) ¶datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

Các lập luận năm, tháng và ngày là bắt buộc. TZINFO có thể là

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, hoặc một trường hợp của một lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Các đối số còn lại phải là số nguyên trong các phạm vi sau:

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    04,

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    05,

  • d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    06,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    04,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    05,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    06,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    07,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    08.

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 sẽ được nêu ra.

Mới trong phiên bản 3.6: Đã thêm đối số

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10.Added the
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 argument.

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

classmethoddatetime.today () ¶ datetime.today()

Trả về DateTime cục bộ hiện tại, với

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

Tương đương với:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
2

Xem thêm

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
13,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14.

Phương pháp này có chức năng tương đương với

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
13, nhưng không có tham số
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
16.

classMethodDateTime.now (tz = none) ¶datetime.now(tz=None)

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

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc không được chỉ định, thì điều này giống như
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
18, nhưng, nếu có thể, cung cấp độ chính xác hơn so với việc đi qua dấu thời gian
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
09 (ví dụ, điều này có thể có thể trên các nền tảng cung cấp chức năng C
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
20).

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì đó phải là một ví dụ của lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 và ngày và thời gian hiện tại được chuyển đổi thành múi giờ của TZ.

Hàm này được ưu tiên hơn

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
18 và
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
24.

classmethoddatetime.utcnow () ¶datetime.utcnow()

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

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

Điều này giống như

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
13, nhưng trả về ngày và giờ UTC hiện tại, như một đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 ngây thơ. Một dữ liệu UTC hiện tại có thể thu được bằng cách gọi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
29. Xem thêm
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
13.

Cảnh báo

Bởi vì các đối tượng ngây thơ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được xử lý bằng nhiều phương pháp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 như thời gian địa phương, nên việc sử dụng các dữ liệu AWARE để thể hiện thời gian trong UTC. Như vậy, cách được đề xuất để tạo một đối tượng đại diện cho thời gian hiện tại trong UTC là gọi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
29.

ClassMethodDateTime.FromTimestamp (Timestamp, TZ = none) ¶datetime.fromtimestamp(timestamp, tz=None)

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
09. Nếu đối số tùy chọn TZ là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

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

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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì đó phải là một ví dụ của lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 và dấu thời gian được chuyển đổi thành múi giờ của TZ.

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14 có thể tăng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32, nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi nền tảng C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 hoặc
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 Chức năng và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 trên lỗi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 hoặc
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42. Điều này thường bị giới hạn trong nhiều năm từ năm 1970 đến năm 2038. Lưu ý rằng trên các hệ thống phi posix bao gồm các giây nhảy trong khái niệm dấu thời gian của họ, những giây nhảy vọt bị bỏ qua bởi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14, và sau đó có thể có hai dấu thời gian khác nhau thứ hai mang lại các đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 giống hệt nhau. Phương pháp này được ưu tiên hơn
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
48.

Đã thay đổi trong phiên bản 3.3: Tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi các hàm C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 hoặc
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42. Tăng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 trên
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 hoặc
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 thất bại.Raise
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 if the timestamp is out of the range of values supported by the platform C
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 or
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 functions. Raise
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 on
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
11 or
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 failure.

Đã thay đổi trong phiên bản 3.6:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14 có thể trả về các trường hợp với
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 được đặt thành 1.
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14 may return instances with
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 set to 1.

classmethoddatetime.utcfromtimestamp (dấu thời gian) ¶ datetime.utcfromtimestamp(timestamp)

Trả về UTC

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 tương ứng với dấu thời gian POSIX, với
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17. (Đối tượng kết quả là ngây thơ.)

Điều này có thể tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32, nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi hàm C
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 trên lỗi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42. Nó phổ biến vì điều này được giới hạn trong nhiều năm từ năm 1970 đến năm 2038.

Để nhận được một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2, hãy gọi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
14:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
3

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
4

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

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8 và
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2.

Cảnh báo

Bởi vì các đối tượng ngây thơ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được xử lý bằng nhiều phương pháp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 như thời gian địa phương, nên việc sử dụng các dữ liệu AWARE để thể hiện thời gian trong UTC. Như vậy, cách được đề xuất để tạo một đối tượng đại diện cho một dấu thời gian cụ thể trong UTC là bằng cách gọi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
72.

Đã thay đổi trong phiên bản 3.3: Tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 nếu dấu thời gian nằm ngoài phạm vi của các giá trị được hỗ trợ bởi hàm C
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42. Tăng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 thay vì
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 trên
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 thất bại.Raise
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 if the timestamp is out of the range of values supported by the platform C
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 function. Raise
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
12 instead of
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 on
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
42 failure.

classmethoddatetime.fromordinal (thứ tự) ¶ datetime.fromordinal(ordinal)

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)
2 tương ứng với thứ tự Gregorian Proporian, trong đó ngày 1 tháng 1 năm 1 có thứ tự 1.
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 được nâng lên trừ khi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
81. Giờ, phút, thứ hai và micro giây của kết quả là tất cả 0 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)
8 là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

classMethodDateTime.combine (ngày, giờ, tzinfo = self.tzinfo) ¶datetime.combine(date, time, tzinfo=self.tzinfo)

Trả về một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 mới có các thành phần ngày bằng với đối tượng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 đã cho và các thành phần thời gian của họ bằng với đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 đã cho. Nếu đối số TZINFO được cung cấp, giá trị của nó được sử dụng để đặt thuộc tí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)
8 của kết quả, nếu không, thuộc tí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)
8 của đối số thời gian được sử dụng.

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

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

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
90. Nếu ngày là đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2, các thành phần thời gian của nó và các thuộc tí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)
8 bị bỏ qua.

Đã thay đổi trong phiên bản 3.6: Đã thêm đối số TZINFO.Added the tzinfo argument.

classmethoddatetime.fromisoformat (date_string) ¶datetime.fromisoformat(date_string)

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)
2 tương ứng với DATE_STRING ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau:

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

  2. Bộ phân cách

    from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    94 có thể được thay thế bằng bất kỳ ký tự unicode nào.

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

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

Examples:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
5

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

classmethoddatetime.fromisocalendar (năm, tuần, ngày) ¶datetime.fromisocalendar(year, week, day)

Trả về mộ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)
2 tương ứng với ngày lịch ISO được chỉ định theo năm, tuần và ngày. Các thành phần không ngày của DateTime được điền với các giá trị mặc định bình thường của chúng. Đây là nghịch đảo của hàm
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
96.

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

classMethodDateTime.strptime (date_string, định dạng) ¶datetime.strptime(date_string, format)

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)
2 tương ứng với Date_String, được phân tích cú pháp theo định dạng.

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
6

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 được nâng lên nếu DATE_STRING và định dạng có thể được phân tích cú pháp bởi
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
99 hoặc nếu nó trả về một giá trị không phải là một thời gian. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem hành vi strftime () và strptime ().strftime() and strptime() Behavior.

Thuộc tính lớp:

datetime.min¶min

Đại diện sớm nhất có thể đại diệ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)
2,
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
01.

DateTime.Max¶max

Đại diện mới nhất có thể đại diệ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)
2,
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
03.

DateTime.Resolution¶resolution

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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 không bình đẳng,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
38.

Thuộc tính thể hiện (chỉ đọc):

DateTime.Year¶year

Từ

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8 đến
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2 bao gồm.

DateTime.month¶month

Từ 1 đến 12 bao gồm.

DateTime.day¶day

Từ 1 đến số ngày trong tháng nhất định của năm nhất định.

DateTime.Hour¶hour

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
08.

DateTime.Minute¶minute

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
09.

DateTime.Second¶¶second

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
09.

DateTime.Second¶¶microsecond

DateTime.MicroSecond¶

Trong
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
11.
tzinfo

datetime.tzinfo¶

Đối tượng được truyền dưới dạng đối số TZINFO cho hàm tạo
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 hoặc
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 nếu không có ai được thông qua.
fold

DateTime.fold¶

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

Hoạt động được hỗ trợ:

Hoạt động

Kết quả

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
15

(1)

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
16

(2)

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
17

(3)

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
18

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)
2 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)
2. (4)

  1. DateTime2 là thời gian bị xóa Timedelta khỏi DateTime1, tiến lên theo thời gian nếu

    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    35> 0 hoặc lùi nếu
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    35 <0. Kết quả có cùng thuộc tí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)
    
    8 như DateTime đầu vào và DateTime2 - DateTime1 == Timedelta sau.
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    32 được nâng lên nếu DateTime2.year sẽ nhỏ hơn
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    8 hoặc lớn hơn
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    2. Lưu ý rằng không có điều chỉnh múi giờ được thực hiện ngay cả khi đầu vào là một đối tượng nhận thức.

  2. Tính toán DateTime2 sao cho DateTime2 + TimEdelTa == DateTime1. Đối với bổ sung, kết quả có cùng thuộc tí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)
    
    8 như DateTime đầu vào và không có điều chỉnh múi giờ nào được thực hiện ngay cả khi đầu vào được biết.

  3. Phép trừ 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)
    
    2 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)
    
    2 chỉ được xác định nếu cả hai toán hạng đều ngây thơ hoặc nếu cả hai đều biết. Nếu một người nhận thức và người kia là ngây thơ,
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 sẽ được nâng lên.

    Nếu cả hai đều ngây thơ hoặc cả hai đều biết và có cùng thuộc tí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)
    
    8, các thuộc tí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)
    
    8 bị bỏ qua và kết quả là đối tượng
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    25 t sao cho
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    34. Không có điều chỉnh múi giờ được thực hiện trong trường hợp này.

    Nếu cả hai đều biết và có các thuộc tí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)
    
    8 khác nhau,
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    36 hoạt động như thể A và B lần đầu tiên được chuyển đổi thành các dữ liệu UTC ngây thơ trước. Kết quả là
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    37 ngoại trừ việc triển khai không bao giờ tràn.

  4. DateTime1 được coi là ít hơn DateTime2 khi DateTime1 đi trước DateTime2 kịp thời.

    Nếu một so sánh là ngây thơ và cái kia được biết,

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 sẽ được nêu ra nếu một so sánh đơn đặt hàng được thử. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng với các trường hợp nhận thức.

    Nếu cả hai so sánh đều biết và có cùng thuộc tí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)
    
    8, thuộc tí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)
    
    8 phổ biến bị bỏ qua và các dữ liệu cơ sở được so sánh. Nếu cả hai so sánh đều biết và có các thuộc tí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)
    
    8 khác nhau, các so sánh trước tiên được điều chỉnh bằng cách trừ các độ lệch UTC của chúng (thu được từ
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    42).

    Đã thay đổi trong phiên bản 3.3: So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ ____62 don don tăng

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95.Equality comparisons between aware and naive
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 instances don’t raise
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95.

    Ghi chú

    Để ngăn chặn so sánh rơi trở lại sơ đồ mặc định so sánh các địa chỉ đối tượng, so sánh DateTime thường tăng

    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 nếu so sánh khác cũng không phải là đối tượng
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=10,
    ...     milliseconds=29000,
    ...     minutes=5,
    ...     hours=8,
    ...     weeks=2
    ... )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    2. Tuy nhiên,
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    55 được trả về thay vào đó nếu so sánh khác có thuộc tính
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    56. Móc này cung cấp cho các loại đối tượng ngày khác cơ hội thực hiện so sánh loại hỗn hợp. Nếu không, khi một đối tượng
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=10,
    ...     milliseconds=29000,
    ...     minutes=5,
    ...     hours=8,
    ...     weeks=2
    ... )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    2 được so sánh với một đối tượng thuộc loại khác,
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    95 được nâng lên trừ khi so sánh là
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    89 hoặc
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    90. Các trường hợp sau trở lại lần lượt là
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    61 hoặc
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    62.

Phương pháp ví dụ:

datetime.date () ¶date()

Trả về đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 với cùng năm, tháng và ngày.

datetime.time () ¶time()

Trả về đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 với cùng một giờ, phút, thứ hai, micro giây và gấp.
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17. Xem thêm Phương pháp
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
59.

Đã thay đổi trong phiên bản 3.6: Giá trị gấp được sao chép vào đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 đã trả về.The fold value is copied to the returned
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 object.

datetime.timetz ()timetz()

Trả về đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 với các thuộc tính cùng giờ, phút, thứ hai, microsecond, gấp và tzinfo. Xem thêm Phương thức
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
62.

Đã thay đổi trong phiên bản 3.6: Giá trị gấp được sao chép vào đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 đã trả về.The fold value is copied to the returned
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 object.

datetime.timetz ()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ề đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 với các thuộc tính cùng giờ, phút, thứ hai, microsecond, gấp và tzinfo. Xem thêm Phương thức
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
62.

datetime.Reater (năm = self.year, tháng = self.month, day = self.day, giờ = self.hour, phút = self.minute, second = self.second, microsecond = self.microsecond, tzinfo = self. tzinfo, *, nếp gấp = 0) ¶Added the

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 argument.

Trả về một datetime với cùng một thuộc tính, ngoại trừ các thuộc tính đó được cung cấp các giá trị mới bởi bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
64 có thể được chỉ định để tạo một dữ liệu ngây thơ từ một DateTime Aware không có chuyển đổi dữ liệu ngày và thời gian.
astimezone(tz=None)

Mới trong phiên bản 3.6: Đã thêm đối số

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10.

datetime.astimezone (tz = none) ¶

Trả về một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 với thuộc tí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)
8 mới TZ, điều chỉnh dữ liệu ngày và thời gian để kết quả là thời gian UTC giống như tự, nhưng trong thời gian địa phương của TZ.

Nếu được cung cấp, TZ phải là một ví dụ của lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 và các phương thức
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 và
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 của nó không được trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17. Nếu bản thân là ngây thơ, nó được cho là đại diện cho thời gian trong timezone hệ thống.

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

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
80. Nếu bạn chỉ muốn xóa đối tượng múi giờ khỏi DT DTETIME AWARE mà không cần chuyển đổi dữ liệu ngày và thời gian, hãy sử dụng
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
81.

Lưu ý rằng phương thức

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
82 mặc định có thể được ghi đè trong lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 để ảnh hưởng đến kết quả được trả về bởi
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84. Bỏ qua các trường hợp lỗi,
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 hành động như:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
7

Đã thay đổi trong phiên bản 3.3: TZ hiện có thể được bỏ qua.tz now can be omitted.

Đã thay đổi trong phiên bản 3.6: Phương pháp

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 hiện có thể được gọi trên các trường hợp ngây thơ được cho là đại diện cho hệ thống thời gian cục bộ.The
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 method can now be called on naive instances that are presumed to represent system local time.

datetime.utcoffset () ¶utcoffset()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
90 và tăng một ngoại lệ nếu sau này không trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 với cường độ ít hơn một ngày.

Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

datetime.dst () ¶dst()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
96 và tăng một ngoại lệ nếu sau này không trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 với cường độ ít hơn một ngày.

Đã thay đổi trong phiên bản 3.7: bù DST không bị giới hạn trong một số phút.The DST offset is not restricted to a whole number of minutes.

datetime.tzname ()tzname()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, hãy trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
02, sẽ tăng một ngoại lệ nếu cái sau không trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc một đối tượng chuỗi,

datetime.timetuple () ¶timetuple()

Trả về một

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
64 như được trả về bởi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
65.

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
66 tương đương với:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
8

trong đó

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
67 là số ngày trong năm hiện tại bắt đầu với
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9 cho ngày 1 tháng 1. Cờ
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 của kết quả được đặt theo phương pháp
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70:
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 Trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17,
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 được đặt thành
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
16; khác nếu
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 trả về giá trị khác không,
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 được đặt thành
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9; Khác
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 được đặt thành
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
28.

datetime.utctimetuple () ¶utctimetuple()

Nếu ví dụ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 D là ngây thơ, thì điều này giống như
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
66 ngoại trừ
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 bị buộc đến 0 bất kể
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
25 trả về. DST không bao giờ có hiệu lực cho thời gian UTC.

Nếu D nhận thức được, D được chuẩn hóa theo thời gian UTC, bằng cách trừ

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
26 và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
64 cho thời gian chuẩn hóa được trả về.
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 bị buộc đến 0. Lưu ý rằng một
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 có thể được nâng lên nếu D.Year là
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
8 hoặc
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
2 và điều chỉnh UTC tràn ra trong ranh giới một năm.

Cảnh báo

Bởi vì các đối tượng ngây thơ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được xử lý bằng nhiều phương pháp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 như thời gian địa phương, nên việc sử dụng các dữ liệu AWARE để thể hiện thời gian trong UTC; Do đó, sử dụng
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
34 có thể cho kết quả sai lệch. Nếu bạn có mộ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)
2 ngây thơ đại diện cho UTC, hãy sử dụng
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
36 để làm cho nó biết, tại thời điểm đó bạn có thể sử dụng
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
37.

datetime.toordinal () ¶toordinal()

Trả lại thứ tự Gregorian của ngày. Giống như

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
38.

datetime.timestamp ()timestamp()

Trả về dấu thời gian POSIX tương ứng với ví dụ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2. Giá trị trả về là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
52 tương tự như được trả về bởi
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
09.

Các trường hợp ngây thơ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được giả định là biểu diễn thời gian cục bộ và phương pháp này dựa trên hàm c
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
43 để thực hiện chuyển đổi. 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)
2 hỗ trợ phạm vi giá trị rộng hơn
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
43 trên nhiều nền tảng, phương pháp này có thể tăng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
32 cho thời gian xa trong quá khứ hoặc xa trong tương lai.

Để biết các trường hợp

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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, giá trị trả về được tính là:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
9

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

Đã thay đổi trong phiên bản 3.6: Phương thức

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
48 sử dụng thuộc tính
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 để phân loại thời gian trong một khoảng thời gian lặp lại.The
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
48 method uses the
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 attribute to disambiguate the times during a repeated interval.

Ghi chú

Không có phương pháp nào để có được dấu thời gian POSIX trực tiếp từ một trường hợp

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 ngây thơ đại diện cho thời gian UTC. Nếu ứng dụng của bạn sử dụng quy ước này và timezone hệ thống của bạn không được đặt thành UTC, bạn có thể có được dấu thời gian POSIX bằng cách cung cấp
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
51:

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
0

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

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
1

datetime.weekday () ¶weekday()

Trả lại ngày trong tuần với tư cách là một số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. giống như

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
52. Xem thêm
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
72.

datetime.isoweekday () ¶isoweekday()

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

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
54. Xem thêm
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
74,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
75.

datetime.isocalendar () ¶isocalendar()

Trả về một tuple có tên với ba thành phần:

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
77 và
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
78. Giống như
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
60.named tuple with three components:
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
5,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
77 and
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
78. The same as
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
60.

datetime.isoformat (sep = 't', timespec = 'auto') ¶isoformat(sep='T', timespec='auto')

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

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    61, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 không phải là 0

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    63, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0

Nếu

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 không trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, một chuỗi được thêm vào, cho phép bù UTC:

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    67, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 không phải là 0

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    69, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0

Examples:

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
2

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

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
71) là bộ phân cách một ký tự, được đặt giữa các phần ngày và thời gian của kết quả. Ví dụ:

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
3

Thời gian đối số tùy chọn chỉ định số lượng các thành phần bổ sung của thời gian bao gồm (mặc định là

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
72). Nó có thể là một trong những điều sau đây:

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    72: Giống như
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    74 nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0, giống như
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    76 khác.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    77: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    79 hai chữ số.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    80: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    9 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    83.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    74: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    9 và
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    0 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    88.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    89: Bao gồm toàn thời gian, nhưng cắt phần thứ hai của phân số thành mili giây. Định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    90.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    76: Bao gồm toàn thời gian ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    92.

Ghi chú

Các thành phần thời gian bị loại trừ bị cắt ngắn, không tròn.

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 sẽ được nêu ra trên một đối số thời gian không hợp lệ:

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
4

Mới trong phiên bản 3.6: Đã thêm đối số TimesPEC.Added the timespec argument.

DateTime .__ str __ () ¶__str__()

Đối với một ví dụ

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 D,
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
80 tương đương với
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
96.

datetime.ctime () ¶ctime()

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

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
5

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
82 tương đương với:

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
9

Trên các nền tảng nơi hàm C

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
83 gốc (
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
84 gọi, như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)
00 không gọi) phù hợp với tiêu chuẩn C.

datetime.strftime (định dạng) ¶strftime(format)

Trả về một chuỗi đại diện cho ngày và thời gian, đượ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 hành vi strftime () và strptime ().strftime() and strptime() Behavior.

DateTime .__ Định dạng __ (Định dạng) ¶__format__(format)

Giống 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)
01. Điều này cho phép chỉ định một chuỗi định dạng cho một đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 trong các chuỗi chuỗi được định dạng và khi sử dụng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem hành vi strftime () và strptime ().formatted string literals and when using
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. For a complete list of formatting directives, see strftime() and strptime() Behavior.

Ví dụ về việc sử dụng: ________ 62¶

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

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

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
7

Ví dụ dưới đây xác định một lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 nắm bắt thông tin múi giờ cho Kabul, Afghanistan, đã sử dụng +4 UTC cho đến năm 1945 và sau đó +4: 30 UTC sau đó:

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
8

Việc sử dụng

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

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
9

>>> from datetime import timedelta >>> delta = timedelta( ... days=50, ... seconds=27, ... microseconds=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 Đối tượng

Đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 đạ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 đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8.

classDateTime.time (giờ = 0, phút = 0, thứ hai = 0, microsecond = 0, tzinfo = none, *, nếp gấp = 0) ¶datetime.time(hour=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 date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, hoặc một trường hợp của một lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Các đối số còn lại phải là số nguyên trong các phạm vi sau:

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    04,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    05,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    06,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    07,

  • from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    08.

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

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 sẽ được nêu ra. Tất cả mặc định là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
28 ngoại trừ tzinfo, mặc định là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

Thuộc tính lớp:

thời gian.min¶min

Đại diện sớm nhất có thể đại diệ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)
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)
22.

thời gian.max¶max

Các đại diện mới nhất có thể đại diệ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)
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)
24.

Thời gian.Resolution¶resolution

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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 không bình đẳng,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
38, mặc dù lưu ý rằng số học trên các đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 không được hỗ trợ.

Thuộc tính thể hiện (chỉ đọc):

thời gian.hour¶hour

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
08.

thời gian.minute¶minute

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
09.

thời gian.second¶¶second

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
09.

thời gian.second¶¶microsecond

thời gian.microsecond¶

Trong
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
11.
tzinfo

thời gian.tzinfo¶

Đối tượng được truyền dưới dạng đối số TZINFO cho hàm tạo
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 hoặc
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 nếu không có ai được thông qua.
fold

thời gian.fold¶

Trong

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
14. Được sử dụng để phân loại thời gian tường trong một khoảng thời gian lặp đi lặp lại. . Đại diện thời gian tường giống nhau.

Mới trong phiên bản 3.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)
4 Các đối tượng hỗ trợ 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)
4 với
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4, trong đó A được coi là ít hơn B khi A trước B trong thời gian. Nếu một so sánh là ngây thơ và cái kia được biết,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
95 sẽ được nêu ra nếu một so sánh đơn đặt hàng được thử. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng với các trường hợp nhận thức.

Đã thay đổi trong phiên bản 3.3: So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ ____64 don don tăng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
95.Equality comparisons between aware and naive
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 instances don’t raise
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
95.

Trong bối cảnh Boolean, một đối tượng

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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được coi là sai nếu nó đại diện cho nửa đêm ở 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 đủ.Before Python 3.5, 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)
4 object was considered to be false if it represented midnight in UTC. This behavior was considered obscure and error-prone and has been removed in Python 3.5. See bpo-13936 for full details.

Hàm tạo khác:

classmethodtime.fromisoformat (Time_String) ¶ time.fromisoformat(time_string)

Trả về mộ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)
4 tương ứng với thời gian_String ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau:

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

  2. from datetime import datetime
    
    # datetime object containing current date and time
    now = datetime.now()
     
    print("now =", now)
    
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)	
    
    94 hàng đầu, thường được yêu cầu trong trường hợp có thể có sự mơ hồ giữa ngày và thời gian, là không cần thiết.

  3. Giây phân số có thể có bất kỳ số lượng chữ số nào (bất cứ thứ gì ngoài 6 sẽ bị cắt ngắn).

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

Examples:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
0

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

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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
55.Previously, this method only supported formats that could be emitted by
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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.

Phương pháp ví dụ:

Time.Replace (giờ = self.hour, phút = self.minute, second = self.second, microsecond = self.microsecond, tzinfo = self.tzinfo, *, gấp = 0) ¶replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)

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)
4 với cùng một giá trị, ngoại trừ các thuộc tính đó được đưa ra các giá trị mới bởi bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
64 có thể được chỉ định để tạo
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 ngây thơ từ AWARE
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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, mà không chuyển đổi dữ liệu thời gian.

Mới trong phiên bản 3.6: Đã thêm đối số

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10.Added the
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 argument.

Time.isoformat (TimesPec = 'Auto') ¶isoformat(timespec='auto')

Trả về một chuỗi biểu thị thời gian ở định dạng ISO 8601, một trong số:

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    92, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 không phải là 0

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    88, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=10,
    ...     milliseconds=29000,
    ...     minutes=5,
    ...     hours=8,
    ...     weeks=2
    ... )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    65, nếu
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    69 không trả lại
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    17

  • >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=10,
    ...     milliseconds=29000,
    ...     minutes=5,
    ...     hours=8,
    ...     weeks=2
    ... )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    68, nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0 và
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    69 không trả lại
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    17

Thời gian đối số tùy chọn chỉ định số lượng các thành phần bổ sung của thời gian bao gồm (mặc định là

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
72). Nó có thể là một trong những điều sau đây:

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    72: Giống như
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    74 nếu
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    1 là 0, giống như
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    76 khác.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    77: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    79 hai chữ số.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    80: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    9 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    83.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    74: Bao gồm
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    8,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    9 và
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    0 ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    88.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    89: Bao gồm toàn thời gian, nhưng cắt phần thứ hai của phân số thành mili giây. Định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    90.

  • object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    76: Bao gồm toàn thời gian ở định dạng
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    92.

Ghi chú

Các thành phần thời gian bị loại trừ bị cắt ngắn, không tròn.

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 sẽ được nêu ra trên một đối số thời gian không hợp lệ.

Example:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
1

Mới trong phiên bản 3.6: Đã thêm đối số TimesPEC.Added the timespec argument.

thời gian .__ str __ ()__str__()

Trong một thời gian t,

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
69 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)
95.

Time.strftime (định dạng) ¶strftime(format)

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. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem hành vi strftime () và strptime ().strftime() and strptime() Behavior.

Thời gian .__ Định dạng __ (Định dạng) ¶__format__(format)

Giống 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)
96. Điều này cho phép chỉ định một chuỗi định dạng cho một đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 trong các chuỗi chuỗi được định dạng và khi sử dụng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem hành vi strftime () và strptime ().formatted string literals and when using
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
88. For a complete list of formatting directives, see strftime() and strptime() Behavior.

thời gian.utcoffset () ¶utcoffset()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
02 và tăng một ngoại lệ nếu sau này không trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 với cường độ ít hơn một ngày.

Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

thời gian.dst () ¶dst()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
08 và tăng một ngoại lệ nếu sau này không trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 với cường độ ít hơn một ngày.

Đã thay đổi trong phiên bản 3.7: bù DST không bị giới hạn trong một số phút.The DST offset is not restricted to a whole number of minutes.

thời gian.tzname ()tzname()

Nếu

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

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17, thì khác sẽ trả về
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
14 hoặc tăng một ngoại lệ nếu sau này không trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc một đối tượng chuỗi.

Ví dụ về việc sử dụng: ________ 64¶

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

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

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
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) 8 Đối tượng

classdatetime.tzinfo¶datetime.tzinfo

Đây là một lớp cơ sở trừu tượng, có nghĩa là lớp này không nên được khởi tạo trực tiếp. Xác định một lớp con 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)
8 để 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)

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 có thể được chuyển cho các hàm tạo cho các đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 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)
4. Các đối tượng thứ hai xem các thuộc tính của chúng là trong thời gian địa phương và đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8 hỗ trợ các phương thức tiết lộ sự bù đắp của thời gian địa phương từ UTC, tên của múi giờ và độ lệch DST, tất cả đều liên quan đến một ngày hoặc đối tượng thời gian được truyền cho chúng.

Bạn cần lấy một lớp con cụ thể và (ít nhất) việc thực hiện các phương pháp

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 tiêu chuẩn cần thiết cho các phương pháp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 bạn sử dụng. Mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 cung cấp
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2, một lớp con bê tông đơn giản 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)
8 có thể đại diện cho múi giờ với phần bù cố định từ UTC như UTC hoặc Bắc Mỹ EST và EDT.

Yêu cầu đặc biệt đối với Pickling: Một lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 phải có phương pháp
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
30 có thể được gọi mà không có đối số, nếu không nó có thể được ngâm nhưng có thể không được giải phóng nữa. Đây là một 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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 có thể cần phải thực hiện các phương pháp sau. Chính xác thì các phương pháp cần thiết phụ thuộc vào việc sử dụng các đối tượng Aware
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Nếu nghi ngờ, chỉ cần thực hiện tất cả chúng.

tzinfo.utcoffset (DT) ¶utcoffset(dt)

Trả lại thời gian địa phương từ UTC, như một đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 dương ở phía đông của UTC. Nếu giờ địa phương là phía tây của UTC, điều này sẽ là âm.

Điều này thể hiện tổng số bù từ UTC; Ví dụ: nếu đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8 đại diện cho cả hai điều chỉnh múi giờ và DST,
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 sẽ trả lại tổng của chúng. Nếu OFC Offset không được biết, hãy trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17. Khác, giá trị được trả về phải là một đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 hoàn toàn trong khoảng từ
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
38 và
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
39 (độ lớn của phần bù phải ít hơn một ngày). Hầu hết các triển khai của
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 có thể sẽ trông giống như một trong hai điều này:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
3

Nếu

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 không trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17,
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 cũng không được trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

Việc triển khai mặc định của

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 tăng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
46.

Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

tzinfo.dst (dt) ¶dst(dt)

Trả về đ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

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 hoặc
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 nếu thông tin DST không được biết đến.

Trả lại

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
97 nếu DST không có hiệu lực. Nếu DST có hiệu lực, hãy trả về phần bù dưới dạng đối tượng
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 (xem
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69 để biết chi tiết). Lưu ý rằng Offset DST, nếu có, đã được thêm vào phần bù UTC được trả về bởi
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
69, do đó, không cần phải tham khảo
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 trừ khi bạn quan tâm đến việc lấy thông tin DST một cách riêng biệt. Ví dụ:
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
37 gọi phương 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)
8 của nó là phương thức ____ ____470 để xác định cách đặt cờ
object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
09 và
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
82 gọi
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 để tính các thay đổi DST khi băng qua Zones Zones.

Một thể hiện của một lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 mô hình hóa cả thời gian tiêu chuẩn và ánh sáng ban ngày phải nhất quán theo nghĩa này:

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

Phải trả về kết quả tương tự cho mỗ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)
2 dt với
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
63 cho các lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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, biểu thức này mang lại múi giờ, không nên phụ thuộc vào ngày hoặc thời gian, mà chỉ vào vị trí địa lý. Việc thực hiện
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
65 phụ thuộc vào điều này, nhưng không thể phát hiện các vi phạm; Nó có trách nhiệm của lập trình viên để đảm bảo nó. Nếu lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 không thể đảm bảo điều này, nó có thể ghi đè lên việc triển khai mặc định của
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
82 để hoạt động chính xác với
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 bất kể.

Hầu hết các triển khai của

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 có thể sẽ trông giống như một trong hai điều này:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
4

or:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
5

Việc triển khai mặc định của

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
70 tăng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
46.

Đã thay đổi trong phiên bản 3.7: bù DST không bị giới hạn trong một số phút.The DST offset is not restricted to a whole number of minutes.

tzinfo.tzname (dt) ¶tzname(dt)

Trả về tên múi giờ tương ứng với đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 DT, dưới dạng chuỗi. Không có gì về tên chuỗi được xác định bởi mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 và ở đó, không có yêu cầu nào có nghĩa là bất cứ điều gì cụ thể. Ví dụ, GMT ,, UTC, UTC ,, -500 ,, -5 Trả về
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 nếu một tên chuỗi được biết đến. Lưu ý rằng đây là một phương thức chứ không phải là một chuỗi cố định chủ yếu vì một số lớp con
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 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 là nếu lớp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 đang tính đến thời gian ban ngày.

Việc triển khai mặc định của

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
77 tăng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
46.

Các phương pháp này được gọi bởi một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 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)
4, để đáp ứng với các phương thức cùng tên của chúng. Một đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 tự chuyển thành đối số và đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 chuyển
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 làm đối số. Do đó, một phương pháp phân lớp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 nên được chuẩn bị để chấp nhận đối số DT là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 hoặc của lớp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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.

Khi

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 được thông qua, nó sẽ giúp nhà thiết kế lớp học quyết định phản hồi tốt nhất. Ví dụ: trả lại
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17 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
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Nó có thể hữu ích hơn cho
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
90 để 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

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 được truyền để đáp ứng với phương 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)
2,
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
93 là cùng một đối tượng với bản thân. Các phương 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)
8 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
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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. Mục đích là các phương pháp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 diễn giải DT là ở thời điểm địa phương và không cần phải lo lắng về các đối tượng trong thời gian khác.

Có thêm một phương pháp

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 mà một lớp con có thể muốn ghi đè:

tzinfo.fromutc (dt) ¶fromutc(dt)

Điều này được gọi từ triển khai

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
65 mặc định. Khi được gọi từ đó,
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
93 là bản thân và dữ liệu ngày và thời gian của DT, sẽ được xem là thể hiện thời gian UTC. Mục đích của
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
00 là để điều chỉnh dữ liệu ngày và giờ, trả về một thời gian tương đương trong thời gian địa phương.

Hầu hết các lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 sẽ có thể kế thừa việc triển khai
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
00 mặc định mà không gặp vấn đề gì. Nó đủ mạnh để xử lý các múi giờ bù cố định và múi giờ chiếm cả thời gian chuẩn và ánh sáng ban ngày, và sau đó ngay cả khi thời gian chuyển tiếp 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
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
00 mặc định có thể không xử lý chính xác trong mọi trường hợp là một trong đó độ bù tiêu chuẩn (từ UTC) phụ thuộc vào ngày và thời gian cụ thể đã xảy ra, đ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
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 và
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
00 có thể không tạo ra kết quả bạn muốn nếu kết quả là một trong những giờ đi ngang ngay khi thay đổi bù tiêu chuẩn.

Bỏ qua mã cho các trường hợp lỗi, triển khai

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
00 mặc định hoạt động như:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
6

Trong tệp

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
07 sau đây, có một số ví dụ về các lớp
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
7

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 lớp con

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 chiếm cả thời gian tiêu chuẩn và ánh sáng ban ngày, tại các điểm chuyển tiếp DST. Đối với sự cụ thể, hãy xem xét Hoa Kỳ (UTC -0500), nơi EDT bắt đầu phút sau 1:59 (EST) vào Chủ nhật thứ hai vào tháng 3 và kết thúc phút sau 1:59 (EDT) vào Chủ nhật đầu tiên vào tháng 11:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
8

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

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
10 đã giành được kết quả với
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
11 vào ngày DST bắt đầu. Ví dụ, vào mùa xuân chuyển tiếp chuyển tiếp năm 2016, chúng tôi nhận được:

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
9

Khi DST kết thúc (dòng cuối cùng của người Hồi giáo), có một vấn đề có khả năng tồi tệ hơn: Có một giờ có thể được đánh vần rõ ràng trong thời gian tường địa phương: giờ cuối cùng của ban ngày. Ở phía đông, thời gian của Mẫu 5: MM UTC vào ban ngày vào ban ngày kết thúc. Đồng hồ treo tường địa phương nhảy từ 1:59 (giờ ban ngày) trở lại 1:00 (Giờ chuẩn) một lần nữa. Thời gian địa phương của Mẫu 1: MM không rõ ràng.

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
84 bắt chước hành vi đồng hồ cục bộ 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ủa Mẫu 5: mm và 6: mm đều bản đồ thành 1: mm khi được chuyển đổi sang phương Đông, nhưng thời gian sớm hơn có thuộc tính
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 được đặt thành 0 và thời gian sau đó được đặt thành 1. Ví dụ: , vào mùa thu trở lại năm 2016, chúng tôi nhận được:

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime
0

Lưu ý rằng các trường hợp

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 chỉ khác nhau bởi giá trị của thuộc tính
from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	
10 được coi là bằng nhau trong so sánh.

Các ứng dụng có thể mang tính mơ hồ trong thời gian tường nên kiểm tra rõ ràng giá trị của thuộc tính ____310 hoặc tránh sử dụng các lớp con lai

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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; Không có sự mơ hồ khi sử dụng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 hoặc bất kỳ lớp con offset cố định nào khác (chẳng hạn như lớp chỉ đại diện cho EST (bù cố định -5 giờ) hoặc chỉ EDT (bù cố định -4 giờ)).

Xem thêm

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

Mô -đun

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 có lớp
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 cơ bản (để xử lý các lần bù cố định tùy ý từ UTC) và thuộc tính
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
23 của nó (một thể hiện múi giờ UTC).

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 mang cơ sở dữ liệu IANA Timezone (còn được gọi là cơ sở dữ liệu Olson) cho Python và nên sử dụng nó được khuyến nghị.

Cơ sở dữ liệu IANA Timezone

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 đại diện cho lịch sử thời gian địa phương cho nhiều địa điểm đại diện trên toàn cầu. Nó được cập nhật định kỳ để phản ánh những thay đổi được thực hiện bởi các cơ quan chính trị đối với ranh giới múi giờ, độ lệch của UTC và các quy tắc tiết kiệm ánh sáng ban ngày.

>>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) 2 Đối tượng

Lớp

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 là một lớp con 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)
8, mỗi trường hợp thể hiện 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 để đại diện cho thông tin múi giờ ở các địa điểm nơi các độ lệch khác nhau được sử dụng trong các ngày khác nhau trong năm hoặc nơi những thay đổi lịch sử đã được thực hiện cho thời gian dân sự.

classDateTime.timezone (offset, name = none) ¶datetime.timezone(offset, name=None)

Đối số bù phải được chỉ định là đối tượng

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 biểu thị sự khác biệt giữa thời gian địa phương và UTC. Nó phải hoàn toàn giữa
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
38 đến
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
39, nếu không
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07 đượ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ằng phương thức

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

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

Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

timezone.utcoffset (DT) ¶utcoffset(dt)

Trả về giá trị cố định được chỉ định khi thể hiện

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 được xây dựng.

Đối số DT bị bỏ qua. Giá trị trả về là một ví dụ

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 bằng với chênh lệch giữa thời gian địa phương và UTC.

Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

timezone.utcoffset (DT) ¶tzname(dt)

Trả về giá trị cố định được chỉ định khi thể hiện

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
2 được xây dựng.

Đối số DT bị bỏ qua. Giá trị trả về là một ví dụ

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
25 bằng với chênh lệch giữa thời gian địa phương và UTC.

timezone.tzname (DT) ¶Name generated from

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
43 is now plain
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
44, not
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
45.

Nếu tên không được cung cấp trong hàm tạo, tên được trả về bởi
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
36 được tạo từ giá trị của
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
37 như sau. Nếu Offset là
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
97, tên là UT UTC, nếu không thì đó là một chuỗi ở định dạng
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
39, trong đó ± là dấu hiệu của
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
37, HH và MM lần lượt là hai chữ số của
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
41 và
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
42.
dst(dt)

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

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
43 hiện là đơn giản
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
44, không phải
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
45.

timezone.dst (dt) ¶fromutc(dt)

Luôn trả về

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
17.

timezone.fromutc (dt) ¶

Trả lại
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
47. Đối số DT phải là một ví dụ
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 nhận thức, với
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
8 được đặt thành
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
50.
utc

Thuộc tính lớp:

timezone.utc¶

Thời gian UTC,

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

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

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
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)
2 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)
4 đều hỗ trợ phương thức
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
57, để 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.

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

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

Ngược lại, phương thức lớp

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
58 tạo đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 từ một chuỗi đại diện cho một ngày và thời gian và chuỗi định dạng tương ứng.

Bảng dưới đây cung cấp một so sánh cấp cao của

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
52 so với
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
53:

Cách sử dụng

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

Phân tích một chuỗi vào một đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
2 được cho một định dạng tương ứng

Loại phương pháp

Phương thức thể hiện

Phương pháp lớp

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

Phương pháp của

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

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

>>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) 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) 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) 4

Chữ ký

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
52 và
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
53 Mã định dạng

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à các hoạt động này trên tất cả các nền tảng với triển khai C tiêu chuẩn.

Chỉ thị

Nghĩa

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

Thí dụ

Ghi chú

Ngày trong tuần là tên viết tắt địa phương.

(1)

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

Mặt trời, mon, xông, sat (en_us);

Vì vậy, mo, xông, sa (de_de)

Ngày trong tuần là tên đầy đủ của địa phương.

(1)

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

Chủ nhật, Thứ Hai,, Thứ Bảy (EN_US);

Sonntag, Montag, xông, Samstag (de_de)

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

Ngày trong tuần là một số thập phân, trong đó 0 là Chủ nhật và 6 là thứ bảy.

0, 1,…, 6

(9)

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

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

01, 02,…, 31

Tháng là tên địa phương viết tắt tên.

(1)

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

Jan, tháng 2, xông, tháng 12 (en_us);

Jan, tháng 2, xông, Dez (de_de)

Tháng dưới dạng tên đầy đủ của địa phương.

(1)

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

Tháng một, tháng 2,, tháng 12 (en_us);

Januar, Februar, xông, Dezember (de_de)

(9)

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

Tháng dưới dạng số thập phân bằng không.

01, 02,…, 12

(9)

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

Năm không có thế kỷ như một số thập phân số không.

00, 01, xông, 99

(2)

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

Năm với thế kỷ như một số thập phân.

0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999

(9)

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

Giờ (đồng hồ 24 giờ) là một số thập phân bằng không.

Januar, Februar, xông, Dezember (de_de)

(9)

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

Tháng dưới dạng số thập phân bằng không.

01, 02,…, 12

Năm không có thế kỷ như một số thập phân số không.

00, 01, xông, 99

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

Năm với thế kỷ như một số thập phân.

0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999

(9)

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

Giờ (đồng hồ 24 giờ) là một số thập phân bằng không.

0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999

Giờ (đồng hồ 24 giờ) là một số thập phân bằng không.

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

00, 01,…, 23

Giờ (đồng hồ 12 giờ) là một số thập phân bằng không.

(5)

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

Locale từ tương đương với AM hoặc PM.

Am, PM (en_us);

(6)

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

AM, PM (DE_DE)

(1), (3)

(6)

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

Phút như một số thập phân bằng không.

00, 01,…, 59

(9)

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

Thứ hai là một số thập phân bằng không.

(4), (9)

Microsecond là một số thập phân, bằng 0 chữ số đến 6 chữ số.

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

000000, 000001, xông, 999999

(4), (9)

(7), (9)

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

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

Tue ngày 16 tháng 8 21:30:00 1988 (en_us);

DI 16 tháng 8 21:30:00 1988 (DE_DE)

(1)

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

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

16/08/88 (không có);

16/08/1988 (en_us);

16.08.1988 (DE_DE)

(1)

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

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

21:30:00 (en_us);

21:30:00 (de_de)

(1)

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

Một ký tự

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
98 theo nghĩa đen.

Phần trăm

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

Chỉ thị

Nghĩa

Thí dụ

Ghi chú

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

ISO 8601 năm với thế kỷ đại diện cho năm có phần lớn hơn của Tuần lễ ISO (

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

0001, 0002, Mạnh, 2013, 2014, Mạnh, 9998, 9999

(8)

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

ISO 8601 ngày trong tuần là một số thập phân trong đó 1 là thứ Hai.

1, 2,…, 7

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

ISO 8601 tuần là một số thập phân với thứ Hai là ngày đầu tiên trong tuần. Tuần 01 là tuần chứa ngày 4 tháng 1.

01, 02, xông, 53

(8), (9)

Chúng có thể không có sẵn trên tất cả các nền tảng khi được sử dụng với phương pháp

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
52. Các chỉ thị ISO 8601 năm và ISO 8601 không thể thay thế cho các chỉ thị số năm và tuần ở trên. Gọi
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
53 với các chỉ thị ISO 8601 không đầy đủ hoặc mơ hồ sẽ tăng
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
07.

Tập hợp đầy đủ các mã định dạng được hỗ trợ khác nhau giữa các nền tảng, bởi vì Python gọi chức năng thư viện nền tảng C ____ ____8552 và các biến thể nền tảng là phổ biến. Để xem toàn bộ bộ mã định dạng được hỗ trợ trên nền tảng của bạn, hãy tham khảo tài liệu Strftime (3). Cũng có sự khác biệt giữa các nền tảng trong việc xử lý các định dạng định dạng không được hỗ trợ.

Mới trong phiên bản 3.6:

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
99,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
01 và
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
00 đã được thêm vào.
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
99,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
01 and
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
00 were added.

Chi tiết kỹ thuật¶

Nói rộng ra,

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
10 hoạt động như mô -đun
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=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 ____ ____912 Mặc dù không phải tất cả các đối tượng đều hỗ trợ phương thức
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
56.

Đối với phương thức lớp

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
58, giá trị mặc định là
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
15: Bất kỳ thành phần nào không được chỉ định trong chuỗi định dạng sẽ được lấy từ giá trị mặc định. 4

Sử dụng

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
16 tương đương với:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
6

Ngoại trừ khi định dạng bao gồm các thành phần phụ thứ hai hoặc thông tin bù thời gian, được hỗ trợ trong

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
17 nhưng bị loại bỏ bởi
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
18.

Đối với các đối tượng

>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4, mã định dạng cho năm, tháng và ngày không nên được sử dụng, vì các đối tượng
>>> from datetime import timedelta
>>> delta = timedelta(
...     days=50,
...     seconds=27,
...     microseconds=10,
...     milliseconds=29000,
...     minutes=5,
...     hours=8,
...     weeks=2
... )
>>> # Only days, seconds, and microseconds remain
>>> delta
datetime.timedelta(days=64, seconds=29156, microseconds=10)
4 không có giá trị như vậy. Nếu họ sử dụng dù sao,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
21 được thay thế trong năm và
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
9 trong tháng và ngày.

Đối với các đối tượng

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6, không nên sử dụng mã định dạng trong nhiều giờ, phút, giây và micro giây, vì các đối tượng
>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
6 không có giá trị như vậy. Nếu dù sao họ cũng được sử dụng,
from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
28 được thay thế cho họ.

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

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
62 có thể tăng
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
27 hoặc trả lại một chuỗi trống thay thế.

Notes:

  1. Bởi vì định dạng phụ thuộc vào ngôn ngữ hiện tại, nên cẩn thận khi đưa ra các giả định về giá trị đầu ra. Đặt hàng hiện trường sẽ khác nhau (ví dụ: tháng/ngày/năm, so với ngày/tháng/năm) và đầu ra có thể chứa các ký tự unicode được mã hóa bằng cách sử dụng mã hóa mặc định của địa phương (ví dụ: nếu địa phương hiện tại là

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    28, Mã hóa mặc định có thể là bất kỳ một trong số
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    29,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    30 hoặc
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    31; sử dụng
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    32 để xác định mã hóa địa phương hiện tại).

  2. Phương pháp

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53 có thể phân tích các năm trong phạm vi đầy đủ [1, 9999], nhưng năm <1000 phải được chứa bằng 0 đến 4 chữ số.

    Đã thay đổi trong phiên bản 3.2: Trong các phiên bản trước, phương pháp

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    52 đã bị hạn chế ở năm> = 1900.In previous versions,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    52 method was restricted to years >= 1900.

    Đã thay đổi trong phiên bản 3.3: trong Phiên bản 3.2, phương thức

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    52 bị giới hạn ở năm> = 1000.In version 3.2,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    52 method was restricted to years >= 1000.

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

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53, chỉ thị
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    84 chỉ ảnh hưởng đến trường Giờ đầu ra nếu chỉ thị
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    83 được sử dụng để phân tích giờ.

  4. Không giống như mô -đun

    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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, mô -đun
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 không hỗ trợ các giây nhảy.

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

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53, chỉ thị
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    87 chấp nhận từ một đến sáu chữ số và không có miếng đệm bên phải.
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    87 là một phần mở rộng cho tập hợp các ký tự định dạng trong tiêu chuẩn C (nhưng được triển khai riêng trong các đối tượng DateTime và do đó luôn có sẵn).

  6. Đối với một đối tượng ngây thơ, mã định dạng

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    90 được thay thế bằng các chuỗi trống.

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

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

    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    69 được chuyển thành một chuỗi có dạng
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    89, trong đó
    object
        timedelta
        tzinfo
            timezone
        time
        date
            datetime
    
    79 là chuỗi 2 chữ số cho số giờ bù UTC,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    50 là chuỗi 2 chữ số cho số phút bù UTC,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    51 là chuỗi 2 chữ số cho Số lượng giây bù UTC và
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    52 là chuỗi 6 chữ số cho số lượng micro giây bù đắp UTC. Phần
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    52 bị bỏ qua khi phần bù là một số giây và cả phần
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    52 và phần
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    51 bị bỏ qua khi phần bù là toàn bộ số phút. Ví dụ: nếu
    now = 2021-06-25 07:58:56.550604
    date and time = 25/06/2021 07:58:56
    69 trả về
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 được thay thế bằng chuỗi
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    59.

    Đã thay đổi trong phiên bản 3.7: Bồi thường UTC không bị giới hạn trong một số phút.The UTC offset is not restricted to a whole number of minutes.

    Đã thay đổi trong phiên bản 3.7: Khi chỉ thị

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 được cung cấp cho phương pháp
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53, các độ lệch UTC có thể có dấu hai chấm dưới dạng phân tách giữa giờ, phút và giây. Ví dụ,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    62 sẽ được phân tích cú pháp dưới dạng bù một giờ. Ngoài ra, cung cấp
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    63 giống hệt với
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    64.When the
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 directive is provided to the
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53 method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    62 will be parsed as an offset of one hour. In addition, providing
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    63 is identical to
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    64.

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

    Trong

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    52,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    90 được thay thế bằng một chuỗi trống nếu
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    77 trả về
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    17; Nếu không
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    90 được thay thế bằng giá trị trả về, phải là một chuỗi.

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53 chỉ chấp nhận các giá trị nhất định cho
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    90:

    1. Bất kỳ giá trị nào trong

      >>> from datetime import timedelta
      >>> delta1 = timedelta(seconds=57)
      >>> delta2 = timedelta(hours=25, seconds=2)
      >>> delta2 != delta1
      True
      >>> delta2 == 5
      False
      
      73 cho máy của bạn

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

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

    Vì vậy, một người sống ở Nhật Bản có thể có

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    76,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    74 và
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    75 là giá trị hợp lệ, nhưng có lẽ không phải
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    79. Nó sẽ tăng
    d1 = 16/09/2019
    d2 = September 16, 2019
    d3 = 09/16/19
    d4 = Sep-16-2019
    07 cho các giá trị không hợp lệ.

    Đã thay đổi trong phiên bản 3.2: Khi chỉ thị

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 được cung cấp cho phương thức
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53, đối tượng AWARE
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 sẽ được tạo ra.
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 của kết quả sẽ được đặt thành một ví dụ
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    2.When the
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    88 directive is provided to the
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53 method, an aware
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 object will be produced. The
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ...     days=50,
    ...     seconds=27,
    ...     microseconds=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 of the result will be set to a
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    2 instance.

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

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    92 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    93 chỉ được sử dụng trong các tính toán khi ngày trong tuần và năm dương lịch (
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    81) được chỉ định.

  8. Tương tự như

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    92 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    93,
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    00 chỉ được sử dụng trong các tính toán khi ngày trong tuần và năm ISO (
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    99) được chỉ định trong chuỗi định dạng
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53. Cũng lưu ý rằng
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    99 và
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    81 không thể hoán đổi cho nhau.

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

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    53, số 0 hàng đầu là tùy chọn cho các định dạng
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    76,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    79,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    82,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    83,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    85,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    86,
    from datetime import date
    
    today = date.today()
    
    # dd/mm/YY
    d1 = today.strftime("%d/%m/%Y")
    print("d1 =", d1)
    
    # Textual month, day and year	
    d2 = today.strftime("%B %d, %Y")
    print("d2 =", d2)
    
    # mm/dd/y
    d3 = today.strftime("%m/%d/%y")
    print("d3 =", d3)
    
    # Month abbreviation, day and year	
    d4 = today.strftime("%b-%d-%Y")
    print("d4 =", d4)
    
    004,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    92,
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    93. Định dạng
    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    80 không yêu cầu số 0 hàng đầu.

Chú thích

1

Nếu, đó là, chúng ta bỏ qua những ảnh hưởng của thuyết tương đối

2

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

3

Xem hướng dẫn của R. H. Van Gent, về toán học của lịch ISO 8601 để được giải thích tốt.

4

Vượt qua

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
009 sẽ thất bại vì
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
21 không phải là một năm bước nhảy.

DateTime DateTime bây giờ () trong Python là gì?

Ở đây, chúng tôi đã sử dụng DateTime.now () để có được ngày và giờ hiện tại. Sau đó, chúng tôi đã sử dụng strftime () để tạo một chuỗi biểu diễn ngày và thời gian ở định dạng khác.get the current date and time. Then, we used strftime() to create a string representing date and time in another format.

Làm cách nào để sử dụng mô -đun thời gian ngày trong Python?

Cách sử dụng DateTime của Python..
Kết hợp () Nhập DateTime # (giờ, phút) start_time = dateTime.time (7, 0) # (năm, tháng, ngày) start_date = datetime.date (2015, 5, 1) # Tạo một đối tượng DateTime start_datetime = dateTime.datetime.combine (start_date, start_time) ....
Timedelta.....
Dấu thời gian.....
Ngày trong tuần () ....
Chuỗi ngày ..

Làm thế nào để bạn chuyển đổi DD MM thành Yyyy trong Python?

Chúng ta có thể chuyển đổi định dạng chuỗi thành DateTime bằng cách sử dụng hàm strptime ().Chúng tôi sẽ sử dụng định dạng '%Y/%M/%D' để đưa chuỗi vào DateTime ...
Đầu vào là chuỗi DateTime ..
Định dạng là định dạng-'Yyyy-MM-DD'.
DateTime là mô -đun ..

Định dạng DateTime Python là gì?

Một sự kết hợp của một ngày và một thời gian.Các thuộc tính: Năm, tháng, ngày, giờ, phút, thứ hai, micro giây và tzinfo.Thời lượng thể hiện sự khác biệt giữa hai ngày, thời gian hoặc các phiên bản DateTime với độ phân giải micro giây.Một lớp cơ sở trừu tượng cho các đối tượng thông tin múi giờ.. Attributes: year , month , day , hour , minute , second , microsecond , and tzinfo . A duration expressing the difference between two date , time , or datetime instances to microsecond resolution. An abstract base class for time zone information objects.