Mặc dù số học ngày và giờ được hỗ trợ, trọng tâm của việc triển khai là trích xuất thuộc tính hiệu quả để định dạng và thao tác đầu ra
Xem thêm
ModuleGeneral calendar related functions
ModuleTime access and conversions
ModuleConcrete time zones representing the IANA time zone database
Package dateutilThird-party library with expanded time zone and parsing support
Aware and Naive Objects
Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information
With sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation.
A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time [UTC], local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality
For applications requiring aware objects, and objects have an optional time zone information attribute,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4, that can be set to an instance of a subclass of the abstract class. These objects capture information about the offset from UTC time, the time zone name, and whether daylight saving time is in effect
Only one concrete class, the class, is supplied by the module. The class can represent simple timezones with fixed offsets from UTC, such as UTC itself or North American EST and EDT timezones. Supporting timezones at deeper levels of detail is up to the application. The rules for time adjustment across the world are more political than rational, change frequently, and there is no standard suitable for every application aside from UTC
Constants
The module exports the following constants
datetime. MINYEARThe smallest year number allowed in a or object. is
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.05datetime. MAXYEAR
The largest year number allowed in a or object. is
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.09datetime. UTC
Alias for the UTC timezone singleton
New in version 3. 11
Available Types
class datetime. dateAn idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes. , , and
class datetime. timeAn idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds. [There is no notion of “leap seconds” here. ] Attributes. , , , , and
class datetime. datetimeA combination of a date and a time. Attributes. , , , , , , , and
class datetime. timedeltaA duration expressing the difference between two , , or instances to microsecond resolution
class datetime. tzinfoAn abstract base class for time zone information objects. These are used by the and classes to provide a customizable notion of time adjustment [for example, to account for time zone and/or daylight saving time]
class datetime. timezoneA class that implements the abstract base class as a fixed offset from the UTC
New in version 3. 2
Objects of these types are immutable
Subclass relationships
object timedelta tzinfo timezone time date datetime
Common Properties
The , , , and types share these common features
Objects of these types are immutable
Objects of these types are hashable, meaning that they can be used as dictionary keys
Objects of these types support efficient pickling via the module
Determining if an Object is Aware or Naive
Objects of the type are always naive
An object of type or may be aware or naive
A object d is aware if both of the following hold
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
02 is not>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
04 does not return>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03
Otherwise, d is naive
A object t is aware if both of the following hold
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
07 is not>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
09 does not return>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03
Otherwise, t is naive
The distinction between aware and naive doesn’t apply to objects
Objects
Một đối tượng đại diện cho một khoảng thời gian, sự khác biệt giữa hai ngày hoặc thời gian
lớp ngày giờ. timedelta[ngày=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]Tất cả các đối số là tùy chọn và mặc định là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]14. Các đối số có thể là số nguyên hoặc số float và có thể dương hoặc âm
Chỉ ngày, giây và micro giây được lưu trữ nội bộ. Các đối số được chuyển đổi thành các đơn vị đó
Một phần nghìn giây được chuyển đổi thành 1000 micro giây
Một phút được chuyển thành 60 giây
Một giờ được chuyển đổi thành 3600 giây
Một tuần được chuyển đổi thành 7 ngày
và ngày, giây và micro giây sau đó được chuẩn hóa để biểu diễn là duy nhất, với
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
15>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
16 [số giây trong một ngày]>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
17
Ví dụ sau đây minh họa cách mọi đối số ngoài ngày, giây và micro giây được “hợp nhất” và chuẩn hóa thành ba thuộc tính kết quả đó
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
Nếu bất kỳ đối số nào là số float và có micro giây phân số, thì micro giây phân số còn lại từ tất cả các đối số sẽ được kết hợp và tổng của chúng được làm tròn đến micro giây gần nhất bằng cách sử dụng bộ ngắt kết nối từ nửa vòng đến chẵn. Nếu không có đối số nào là float, quá trình chuyển đổi và chuẩn hóa là chính xác [không có thông tin nào bị mất]
Nếu giá trị chuẩn hóa của ngày nằm ngoài phạm vi được chỉ định, được tăng lên
Lưu ý rằng việc chuẩn hóa các giá trị âm lúc đầu có thể gây ngạc nhiên. Ví dụ
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
Thuộc tính lớp
đồng bằng thời gian. phútĐối tượng tiêu cực nhất,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]20đồng bằng thời gian. tối đa
Đối tượng tích cực nhất,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]22đồng bằng thời gian. độ phân giải
Sự khác biệt nhỏ nhất có thể giữa các đối tượng không bằng nhau,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]24
Lưu ý rằng, do chuẩn hóa,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]25 >
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]26.
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]27 không thể biểu diễn như một đối tượng
Thuộc tính phiên bản [chỉ đọc]
Thuộc tính
Giá trị
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]29
Giữa -999999999 và bao gồm cả 999999999
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]30
Bao gồm từ 0 đến 86399
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]31
Bao gồm từ 0 đến 999999
hoạt động được hỗ trợ
Hoạt động
Kết quả
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]32
Tổng của t2 và t3. Sau đó t1-t2 == t3 và t1-t3 == t2 là đúng. [1]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]33
Sự khác biệt của t2 và t3. Sau đó t1 == t2 - t3 và t2 == t1 + t3 là đúng. [1][6]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]34
Delta nhân với một số nguyên. Sau đó t1 // i == t2 là đúng, với điều kiện là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]35
Nói chung, t1 * i == t1 * [i-1] + t1 là đúng. [1]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]36
Delta multiplied by a float. The result is rounded to the nearest multiple of timedelta. resolution using round-half-to-even
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]37
Division [3] of overall duration t2 by interval unit t3. Returns a object
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]39
Delta divided by a float or an int. The result is rounded to the nearest multiple of timedelta. resolution using round-half-to-even
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]40 or
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]41
The floor is computed and the remainder [if any] is thrown away. In the second case, an integer is returned. [3]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]42
The remainder is computed as a object. [3]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]44
Computes the quotient and the remainder.
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]45 [3] and
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]46. q is an integer and r is a object
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]48
Returns a object with the same value. [2]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]50
equivalent to [-t1. days, -t1. seconds, -t1. microseconds], and to t1* -1. [1][4]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]52
equivalent to +t when
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]53, and to -t when
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]54. [2]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]55
Returns a string in the form
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]56, where D is negative for negative
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]57. [5]
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]58
Returns a string representation of the object as a constructor call with canonical attribute values
ghi chú
This is exact but may overflow
This is exact and cannot overflow
Chia cho 0 tăng
-timedelta. max không thể biểu diễn dưới dạng đối tượng
Biểu diễn chuỗi của các đối tượng được chuẩn hóa tương tự như biểu diễn bên trong của chúng. Điều này dẫn đến kết quả hơi bất thường đối với timedelta âm. Ví dụ
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:00
Biểu thức
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
63 sẽ luôn bằng biểu thức>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
64 trừ khi t3 bằng với>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
25;
In addition to the operations listed above, objects support certain additions and subtractions with and objects [see below]
Đã thay đổi trong phiên bản 3. 2. Phép chia tầng và phép chia thực sự của một đối tượng cho một đối tượng khác hiện đã được hỗ trợ, cũng như các phép toán còn lại và hàm. Phép chia thực sự và phép nhân của một đối tượng với một đối tượng hiện được hỗ trợ.
So sánh các đối tượng được hỗ trợ, với một số lưu ý
Các phép so sánh
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]75 hoặc
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]76 luôn trả về a , bất kể loại đối tượng được so sánh
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
Đối với tất cả các phép so sánh khác [chẳng hạn như
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]78 và
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]79], khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
Trong ngữ cảnh Boolean, một đối tượng được coi là đúng khi và chỉ khi nó không bằng
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]83
Phương thức sơ thẩm
timedelta. total_seconds[]Trả về tổng số giây có trong khoảng thời gian. Tương đương với
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]84. Đối với các đơn vị khoảng thời gian không phải là giây, hãy sử dụng biểu mẫu chia trực tiếp [e. g.
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]85]
Lưu ý rằng trong khoảng thời gian rất lớn [hơn 270 năm trên hầu hết các nền tảng], phương pháp này sẽ mất độ chính xác micro giây
New in version 3. 2
Ví dụ về cách sử dụng.
Một ví dụ bổ sung về chuẩn hóa
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.0
Ví dụ về số học
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]
Objects
Một đối tượng đại diện cho một ngày [năm, tháng và ngày] trong lịch lý tưởng hóa, lịch Gregorian hiện tại được mở rộng vô thời hạn theo cả hai hướng
Ngày 1 tháng 1 của năm 1 được gọi là ngày 1, ngày 2 tháng 1 của năm 1 được gọi là ngày 2, v.v.
lớp ngày giờ. ngày[năm , tháng, day]All arguments are required. Arguments must be integers, in the following ranges
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
90>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
91>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
92
If an argument outside those ranges is given, is raised
Other constructors, all class methods
classmethod date. today[]Return the current local date
This is equivalent to
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]94classmethod date. fromtimestamp[timestamp]
Return the local date corresponding to the POSIX timestamp, such as is returned by
This may raise , if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 function, and on
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 failure. It’s common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by
Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 function. Raise instead of on
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 failure. classmethod date. fromordinal[ordinal]
Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1
is raised unless
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]08. For any date d,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]09classmethod date. fromisoformat[date_string]
Return a corresponding to a date_string given in any valid ISO 8601 format, except ordinal dates [e. g.
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]11]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
New in version 3. 7
Changed in version 3. 11. Previously, this method only supported the format
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]12. classmethod date. fromisocalendar[year , week , day]
Return a corresponding to the ISO calendar date specified by year, week and day. This is the inverse of the function
New in version 3. 8
Thuộc tính lớp
date. minThe earliest representable date,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]15date. max
The latest representable date,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]16date. resolution
The smallest possible difference between non-equal date objects,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]17
Thuộc tính phiên bản [chỉ đọc]
date. yearBetween and inclusive
date. monthBetween 1 and 12 inclusive
date. dayBetween 1 and the number of days in the given month of the given year
hoạt động được hỗ trợ
Hoạt động
Kết quả
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]20
date2 will be
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]21 days after date1. [1]
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]22
Computes date2 such that
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]23. [2]
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]24
[3]
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]25
date1 is considered less than date2 when date1 precedes date2 in time. [4]
ghi chú
date2 is moved forward in time if
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
26, or backward if>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
27. Afterward>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
28.>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
29 and>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
30 are ignored. is raised if>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
32 would be smaller than or larger than>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
29 and>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
30 are ignoredThis is exact, and cannot overflow. timedelta. seconds and timedelta. microseconds are 0, and date2 + timedelta == date1 after
In other words,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
25 if and only if>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
38. Date comparison raises if the other comparand isn’t also a object. However,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
41 is returned instead if the other comparand has a>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
42 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when a object is compared to an object of a different type, is raised unless the comparison is>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
75 or>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
76. The latter cases return or , respectively
In Boolean contexts, all objects are considered to be true
Phương thức sơ thẩm
date. replace[year=self. year , month=self. month , day=self. day]Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified
Thí dụ
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]date. timetuple[]
Return a such as returned by
The hours, minutes and seconds are 0, and the DST flag is -1
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]52 is equivalent to
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]0
where
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]53 is the day number within the current year starting with
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.05 for January 1stdate. toordinal[]
Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any object d,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]09date. weekday[]
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]57, a Wednesday. See also date. isoweekday[]
Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]59, a Wednesday. See also , date. isocalendar[]
Return a object with three components.
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]1,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]63 and
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]64
The ISO calendar is a widely used variant of the Gregorian calendar.
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first [Gregorian] calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year
For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]1
Changed in version 3. 9. Result changed from a tuple to a .
ngày. isoformat[]Return a string representing the date in ISO 8601 format,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]12
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]2date. __str__[]
For a date d,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]66 is equivalent to
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]67date. thời gian[]
Return a string representing the date
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]3
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]68 is equivalent to
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]4
on platforms where the native C
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]69 function [which invokes, but which does not invoke] conforms to the C standarddate. strftime[format]
Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. For a complete list of formatting directives, see
date. __format__[format]Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see
Examples of Usage.
Example of counting days to an event
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]5
More examples of working with
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]6
Objects
A object is a single object containing all the information from a object and a object
Like a object, assumes the current Gregorian calendar extended in both directions; like a object, assumes there are exactly 3600*24 seconds in every day
Constructor
class datetime. datetime[year , month , day , hour=0 , minute=0 , second=0 , microsecond=0 , tzinfo=None , * , fold=0]The year, month and day arguments are required. tzinfo may be
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, or an instance of a subclass. The remaining arguments must be integers in the following ranges
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
90,>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
91,>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
92,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
90,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
91,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
92,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
93,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
94
If an argument outside those ranges is given, is raised
New in version 3. 6. Added the
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]96 argument.
Other constructors, all class methods
classmethod datetime. today[]Return the current local datetime, with
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03
Equivalent to
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]7
See also ,
This method is functionally equivalent to , but without a
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0002 parameterclassmethod datetime. now[tz=None]
Return the current local date and time
If optional argument tz is
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 or not specified, this is like , but, if possible, supplies more precision than can be gotten from going through a timestamp [for example, this may be possible on platforms supplying the C
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0006 function]
If tz is not
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, it must be an instance of a subclass, and the current date and time are converted to tz’s time zone
This function is preferred over and
classmethod datetime. utcnow[]Return the current UTC date and time, with
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03
This is like , but returns the current UTC date and time, as a naive object. An aware current UTC datetime can be obtained by calling
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0015. See also
Warning
Because naive
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 objects are treated by many
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0015classmethod datetime. fromtimestamp[timestamp , tz=None]
Return the local date and time corresponding to the POSIX timestamp, such as is returned by . If optional argument tz is
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 or not specified, the timestamp is converted to the platform’s local date and time, and the returned object is naive
If tz is not
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, it must be an instance of a subclass, and the timestamp is converted to tz’s time zone
may raise , if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 or
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 functions, and on
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 or
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 failure. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by , and then it’s possible to have two timestamps differing by a second that yield identical objects. This method is preferred over
Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 or
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 functions. Raise instead of on
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]97 or
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 failure.
Changed in version 3. 6. may return instances with set to 1.
classmethod datetime. utcfromtimestamp[timestamp]Return the UTC corresponding to the POSIX timestamp, with
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03. [The resulting object is naive. ]
This may raise , if the timestamp is out of the range of values supported by the platform C
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 function, and on
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 failure. It’s common for this to be restricted to years in 1970 through 2038
To get an aware object, call
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]8
On the POSIX compliant platforms, it is equivalent to the following expression
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]9
except the latter formula always supports the full years range. between and inclusive
Warning
Because naive
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 objects are treated by many
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0058
Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 function. Raise instead of on
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0028 failure. classmethod datetime. fromordinal[ordinal]
Return the corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. is raised unless
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0067. The hour, minute, second and microsecond of the result are all 0, and is
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03classmethod datetime. combine[date , time , tzinfo=self. tzinfo]
Return a new object whose date components are equal to the given object’s, and whose time components are equal to the given object’s. If the tzinfo argument is provided, its value is used to set the attribute of the result, otherwise the attribute of the time argument is used
For any object d,
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0076. If date is a object, its time components and attributes are ignored
Changed in version 3. 6. Added the tzinfo argument.
classmethod datetime. fromisoformat[date_string]Return a corresponding to a date_string in any valid ISO 8601 format, with the following exceptions
Time zone offsets may have fractional seconds
The
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:00
80 separator may be replaced by any single unicode characterOrdinal dates are not currently supported
Fractional hours and minutes are not supported
Examples
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]0
New in version 3. 7
Changed in version 3. 11. Previously, this method only supported formats that could be emitted by or .
classmethod datetime. fromisocalendar[year , week , day]Return a corresponding to the ISO calendar date specified by year, week and day. The non-date components of the datetime are populated with their normal default values. This is the inverse of the function
New in version 3. 8
classmethod datetime. strptime[date_string , format]Return a corresponding to date_string, parsed according to format
This is equivalent to
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]1
is raised if the date_string and format can’t be parsed by or if it returns a value which isn’t a time tuple. For a complete list of formatting directives, see
Thuộc tính lớp
datetime. minThe earliest representable ,
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0089datetime. max
The latest representable ,
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0091datetime. resolution
Sự khác biệt nhỏ nhất có thể giữa các đối tượng không bằng nhau,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]24
Thuộc tính phiên bản [chỉ đọc]
datetime. yearBetween and inclusive
datetime. monthBetween 1 and 12 inclusive
datetime. dayBetween 1 and the number of days in the given month of the given year
datetime. hourIn
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0096ngày giờ. phút
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0097ngày giờ. giây
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0097ngày giờ. micro giây
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0099ngày giờ. tzinfo
Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 nếu không có đối số nào được truyềnngày giờ. gấp
Trong
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. [Một khoảng thời gian lặp lại xảy ra khi đồng hồ lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] Giá trị 0 [1] thể hiện sớm hơn [muộn hơn] của hai thời điểm có cùng biểu diễn thời gian tường
Mới trong phiên bản 3. 6
hoạt động được hỗ trợ
Hoạt động
Kết quả
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False03
[1]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False04
[2]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False05
[3]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False06
So sánh với. [4]
datetime2 là khoảng thời gian của timedelta bị xóa khỏi datetime1, tiến về phía trước nếu
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
21 > 0 hoặc lùi lại nếu>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
21 < 0. Kết quả có cùng thuộc tính với datetime đầu vào và datetime2 - datetime1 == timedelta sau. được nâng lên nếu datetime2. năm sẽ nhỏ hơn hoặc lớn hơn. Lưu ý rằng không có điều chỉnh múi giờ nào được thực hiện ngay cả khi đầu vào là một đối tượng nhận biếtTính datetime2 sao cho datetime2 + timedelta == datetime1. Ngoài ra, kết quả có cùng thuộc tính với ngày giờ đầu vào và không có điều chỉnh múi giờ nào được thực hiện ngay cả khi đầu vào biết
Phép trừ a từ a chỉ được xác định nếu cả hai toán hạng đều ngây thơ hoặc nếu cả hai đều biết. Nếu một người nhận thức được và người kia ngây thơ, được nuôi dưỡng
Nếu cả hai đều ngây thơ hoặc cả hai đều nhận thức được và có cùng một thuộc tính, các thuộc tính sẽ bị bỏ qua và kết quả là một đối tượng t sao cho
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
22. Không có điều chỉnh múi giờ nào được thực hiện trong trường hợp nàyNếu cả hai đều nhận biết và có các thuộc tính khác nhau, thì ________ 55 ______ 24 hoạt động như thể a và b lần đầu tiên được chuyển đổi thành thời gian biểu UTC ngây thơ trước. Kết quả là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
25 ngoại trừ việc triển khai không bao giờ bị tràndatetime1 được coi là nhỏ hơn datetime2 khi datetime1 trước datetime2 về thời gian
Nếu một so sánh là ngây thơ và người kia nhận thức được, sẽ được nâng lên nếu cố gắng so sánh thứ tự. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng các trường hợp nhận thức
Nếu cả hai đối tượng so sánh đều nhận biết và có cùng thuộc tính, thì thuộc tính chung sẽ bị bỏ qua và thời gian dữ liệu cơ sở được so sánh. Nếu cả hai bộ so sánh đều nhận biết và có các thuộc tính khác nhau, thì các bộ so sánh trước tiên được điều chỉnh bằng cách trừ đi độ lệch UTC của chúng [thu được từ
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
30]Đã thay đổi trong phiên bản 3. 3. So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ không nâng cao.
Ghi chú
Để ngăn việc so sánh quay trở lại sơ đồ so sánh địa chỉ đối tượng mặc định, so sánh ngày giờ thường tăng lên nếu đối tượng so sánh khác cũng không phải là một đối tượng. Tuy nhiên, thay vào đó,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
41 được trả về nếu đối tượng so sánh khác có thuộc tính>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
42. Móc này cung cấp cho các loại đối tượng ngày khác cơ hội thực hiện so sánh kiểu hỗn hợp. Nếu không, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi so sánh là>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
75 hoặc>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
76. Các trường hợp sau trả về hoặc , tương ứng
Phương thức sơ thẩm
ngày giờ. ngày[]Trả về đối tượng có cùng năm, tháng và ngày
ngày giờ. thời gian[]Trả về đối tượng có cùng giờ, phút, giây, micro giây và lần. là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03. Xem thêm phương pháp
Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về.
ngày giờ. timetz[]Trả về đối tượng có cùng thuộc tính giờ, phút, giây, micro giây, nếp gấp và tzinfo. Xem thêm phương pháp
Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về.
ngày giờ. thay thế[năm=chính mình. năm , tháng=mình. tháng , ngày=chính mình. ngày , giờ=chính mình. giờ , phút=chính mình. phút , giây=chính mình. giây , micro giây=chính mình. micro giây , tzinfo=self. tzinfo , * , gấp=0]Trả về một ngày giờ có cùng thuộc tính, ngoại trừ những thuộc tính được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng có thể chỉ định
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False52 để tạo một ngày giờ ngây thơ từ một ngày giờ nhận thức mà không cần chuyển đổi dữ liệu ngày và giờ
New in version 3. 6. Added the
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]96 argument. ngày giờ. múi giờ[tz=Không có]
Trả về một đối tượng có thuộc tính mới tz, điều chỉnh dữ liệu ngày và giờ để kết quả có cùng thời gian UTC với chính nó, nhưng theo giờ địa phương của tz
Nếu được cung cấp, tz phải là một thể hiện của một lớp con và các phương thức and của nó không được trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03. Nếu bản thân là ngây thơ, nó được coi là đại diện cho thời gian trong múi giờ hệ thống
Nếu được gọi mà không có đối số [hoặc với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False60], múi giờ cục bộ của hệ thống được giả định cho múi giờ đích. Thuộc tính
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False61 của phiên bản datetime đã chuyển đổi sẽ được đặt thành phiên bản có tên vùng và phần bù thu được từ HĐH
Nếu
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False63 là tz, thì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False64 là tự. không thực hiện điều chỉnh dữ liệu ngày hoặc giờ. Mặt khác, kết quả là giờ địa phương trong múi giờ tz, biểu thị cùng thời gian UTC với chính mình. sau
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False65,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False66 sẽ có cùng dữ liệu ngày và giờ như
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False67
Nếu bạn chỉ muốn đính kèm một đối tượng múi giờ tz vào một datetime dt mà không cần điều chỉnh dữ liệu ngày và giờ, hãy sử dụng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False68. Nếu bạn chỉ muốn xóa đối tượng múi giờ khỏi dt datetime nhận biết mà không chuyển đổi dữ liệu ngày và giờ, hãy sử dụng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False69
Lưu ý rằng phương thức mặc định có thể được ghi đè trong một lớp con để ảnh hưởng đến kết quả được trả về bởi. Ignoring error cases, acts like
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]2
Đã thay đổi trong phiên bản 3. 3. tz bây giờ có thể được bỏ qua.
Đã thay đổi trong phiên bản 3. 6. Phương thức hiện có thể được gọi trên các phiên bản ngây thơ được cho là đại diện cho giờ địa phương của hệ thống.
ngày giờ. utcoffset[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, nếu không thì trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False78 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng có cường độ nhỏ hơn một ngày
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
ngày giờ. dst[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, nếu không thì trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False84 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng có cường độ nhỏ hơn một ngày
Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.
ngày giờ. tzname[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, ngược lại trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False90, đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng chuỗi,ngày giờ. thời gian[]
Return a such as returned by
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]52 is equivalent to
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]3
trong đó
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]53 là số ngày trong năm hiện tại bắt đầu bằng
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.05 cho ngày 1 tháng 1. Cờ
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False97 của kết quả được thiết lập theo phương thức. là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False97 được đặt thành
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'04; ngày giờ. utctimetuple[]
Nếu trường hợp d là ngây thơ, thì trường hợp này cũng giống như trường hợp
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]52 ngoại trừ trường hợp
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False97 bị buộc về 0 bất kể điều gì mà
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'13 trả về. DST không bao giờ có hiệu lực trong thời gian UTC
Nếu d biết, d được chuẩn hóa thành thời gian UTC, bằng cách trừ đi
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'14 và a cho thời gian chuẩn hóa được trả về.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False97 buộc phải 0. Lưu ý rằng an có thể tăng nếu d. năm là
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.04 hoặc
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.08 và sự điều chỉnh của UTC tràn qua ranh giới năm
Warning
Bởi vì các đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ được nhiều phương thức
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC; . Nếu bạn có một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ đại diện cho UTC, hãy sử dụng
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'24 để làm cho nó biết, tại thời điểm đó bạn có thể sử dụngngày giờ. bình thường[]
Return the proleptic Gregorian ordinal of the date. The same as
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'26datetime. timestamp[]
Return POSIX timestamp corresponding to the instance. The return value is a similar to that returned by
Naive instances are assumed to represent local time and this method relies on the platform C
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'31 function to perform the conversion. Since supports wider range of values than
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'31 on many platforms, this method may raise for times far in the past or far in the future
For aware instances, the return value is computed as
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]4
New in version 3. 3
Đã thay đổi trong phiên bản 3. 6. Phương thức sử dụng thuộc tính để phân biệt thời gian trong một khoảng thời gian lặp lại.
Ghi chú
There is no method to obtain the POSIX timestamp directly from a naive instance representing UTC time. Nếu ứng dụng của bạn sử dụng quy ước này và múi giờ hệ thống của bạn không được đặt thành UTC, thì bạn có thể lấy dấu thời gian POSIX bằng cách cung cấp
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'39
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]5
hoặc bằng cách tính toán dấu thời gian trực tiếp
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]6ngày giờ. ngày trong tuần[]
Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 0 và Chủ Nhật là 6. Giống như
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'40. Xem thêmngày giờ. ngày trong tuần[]
Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 1 và Chủ Nhật là 7. Giống như
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'42. Xem thêm ,ngày giờ. isocalendar[]
Trả về a với ba thành phần.
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]1,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]63 và
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]64. The same as
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'48ngày giờ. isoformat[sep=', timespec='auto']
Trả về một chuỗi biểu thị ngày và giờ ở định dạng ISO 8601
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
49, nếu không phải là 0>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
51, nếu là 0
Nếu không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, một chuỗi sẽ được thêm vào, tạo ra phần bù UTC
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
55, nếu không phải là 0>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
57, nếu là 0
Examples
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]7
Đối số tùy chọn sep [mặc định
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'59] là dấu phân tách một ký tự, được đặt giữa phần ngày và giờ của kết quả. Ví dụ
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]8
Đối số tùy chọn timespec chỉ định số lượng thành phần bổ sung của thời gian cần đưa vào [mặc định là
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'60]. Nó có thể là một trong những điều sau đây
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
60. Giống như>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
62 nếu là 0, giống như>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
64 nếu không>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
65. Bao gồm ở định dạng hai chữ số>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
67>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
68. Bao gồm và ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
71>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
62. Bao gồm , , và ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
76>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
77. Bao gồm toàn thời gian, nhưng cắt ngắn phần thứ hai thành mili giây. Định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
78>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
64. Bao gồm toàn thời gian ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
80
Ghi chú
Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn
sẽ được nâng lên trên một đối số timespec không hợp lệ
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]9
Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian.
ngày giờ. __str__[]Đối với trường hợp d,
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]66 tương đương với
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'84ngày giờ. thời gian[]
Trả về một chuỗi đại diện cho ngày và giờ
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:000
Chuỗi đầu ra sẽ không bao gồm thông tin múi giờ, bất kể đầu vào là nhận biết hay ngây thơ
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]68 is equivalent to
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]4
on platforms where the native C
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]69 function [which invokes, but which does not invoke] conforms to the C standardngày giờ. thời gian chạy[định dạng]
Trả về một chuỗi đại diện cho ngày và giờ, được kiểm soát bởi một chuỗi định dạng rõ ràng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem
ngày giờ. __format__[định dạng]Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see
Examples of Usage.
Ví dụ về làm việc với các đối tượng
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:002
Ví dụ bên dưới định nghĩa một lớp con thu thập thông tin múi giờ cho Kabul, Afghanistan, sử dụng +4 UTC cho đến năm 1945 và sau đó là +4. 30 UTC sau đó
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:003
Cách sử dụng của
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'95 từ phía trên
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:004
Objects
Một đối tượng đại diện cho thời gian [cục bộ] trong ngày, không phụ thuộc vào bất kỳ ngày cụ thể nào và có thể điều chỉnh thông qua một đối tượng
lớp ngày giờ. thời gian[giờ=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0]Tất cả các đối số là tùy chọn. tzinfo có thể là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một thể hiện của lớp con. Các đối số còn lại phải là số nguyên trong các phạm vi sau
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
90,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
91,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
92,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
93,>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]
94
Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên. Tất cả mặc định là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]14 ngoại trừ tzinfo, mặc định là
Thuộc tính lớp
thời gian. phútĐại diện sớm nhất,
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.010thời gian. tối đa
Đại diện mới nhất ,
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.012thời gian. độ phân giải
Sự khác biệt nhỏ nhất có thể giữa các đối tượng không bằng nhau,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]24, mặc dù lưu ý rằng số học trên các đối tượng không được hỗ trợ
Thuộc tính phiên bản [chỉ đọc]
thời gian. giờIn
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0096thời gian. phút
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0097thời gian. giây
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0097thời gian. micro giây
Trong
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:0099thời gian. tzinfo
Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 nếu không có đối số nào được truyềnthời gian. gấp
Trong
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. [Một khoảng thời gian lặp lại xảy ra khi đồng hồ lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] Giá trị 0 [1] thể hiện sớm hơn [muộn hơn] của hai thời điểm có cùng biểu diễn thời gian tường
Mới trong phiên bản 3. 6
các đối tượng hỗ trợ so sánh với , trong đó a được coi là nhỏ hơn b khi a đứng trước b về thời gian. Nếu một so sánh là ngây thơ và người kia nhận thức được, sẽ được nâng lên nếu cố gắng so sánh thứ tự. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng các trường hợp nhận thức
Nếu cả hai đối tượng so sánh đều nhận biết và có cùng thuộc tính, thuộc tính chung sẽ bị bỏ qua và thời gian cơ sở được so sánh. Nếu cả hai bộ so sánh đều nhận biết và có các thuộc tính khác nhau, thì các bộ so sánh trước tiên được điều chỉnh bằng cách trừ đi độ lệch UTC của chúng [thu được từ
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False30]. Để ngăn các so sánh kiểu hỗn hợp quay trở lại so sánh mặc định theo địa chỉ đối tượng, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi phép so sánh là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]75 hoặc
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]76. Các trường hợp sau trả về hoặc , tương ứng
Đã thay đổi trong phiên bản 3. 3. So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ không nâng cao.
Trong ngữ cảnh Boolean, một đối tượng luôn được coi là đúng
Đã thay đổi trong phiên bản 3. 5. Trước Python 3. 5, một đối tượng được coi là sai nếu nó biểu thị nửa đêm theo giờ UTC. Hành vi này được coi là tối nghĩa và dễ bị lỗi và đã bị xóa trong Python 3. 5. Xem bpo-13936 để biết chi tiết đầy đủ.
nhà xây dựng khác
phương pháp phân lớp thời gian. fromisoformat[time_string]Trả về một tương ứng với một time_string ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau
Time zone offsets may have fractional seconds
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:00
80 ở đầu, thường được yêu cầu trong trường hợp có thể có sự không rõ ràng giữa ngày và giờ, không bắt buộcPhân số giây có thể có bất kỳ số lượng chữ số nào [bất kỳ số nào vượt quá 6 sẽ bị cắt bớt]
Fractional hours and minutes are not supported
Examples
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:005
New in version 3. 7
Đã thay đổi trong phiên bản 3. 11. Trước đây, phương pháp này chỉ hỗ trợ các định dạng có thể được phát ra bởi.
Phương thức sơ thẩm
thời gian. thay thế[giờ=chính mình. giờ , phút=chính mình. phút , giây=chính mình. giây , micro giây=chính mình. micro giây , tzinfo=self. tzinfo , * , gấp=0]Trả về a có cùng giá trị, ngoại trừ các thuộc tính được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng có thể chỉ định
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False52 để tạo ngây thơ từ một nhận thức mà không cần chuyển đổi dữ liệu thời gian
New in version 3. 6. Added the
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]96 argument. thời gian. isoformat[timespec=']
Trả về một chuỗi biểu thị thời gian ở định dạng ISO 8601, một trong
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
80, nếu không phải là 0>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
76, nếu là 0>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.0
53, nếu không trả lại>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.0
56, nếu là 0 và không trả về>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03
Đối số tùy chọn timespec chỉ định số lượng thành phần bổ sung của thời gian cần đưa vào [mặc định là
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'60]. Nó có thể là một trong những điều sau đây
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
60. Giống như>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
62 nếu là 0, giống như>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
64 nếu không>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
65. Bao gồm ở định dạng hai chữ số>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
67>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
68. Bao gồm và ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
71>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
62. Bao gồm , , và ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
76>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
77. Bao gồm toàn thời gian, nhưng cắt ngắn phần thứ hai thành mili giây. Định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
78>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
64. Bao gồm toàn thời gian ở định dạng>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
80
Ghi chú
Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn
sẽ được nâng lên trên một đối số timespec không hợp lệ
Thí dụ
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:006
Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian.
thời gian. __str__[]Trong thời gian t,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]55 tương đương với
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.083thời gian. thời gian chạy[định dạng]
Trả về một chuỗi đại diện cho thời gian, được kiểm soát bởi một chuỗi định dạng rõ ràng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem
thời gian. __format__[định dạng]Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see
thời gian. utcoffset[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, nếu không thì trả về
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.090 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng có cường độ nhỏ hơn một ngày
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
thời gian. dst[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, nếu không thì trả về
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.096 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng có cường độ nhỏ hơn một ngày
Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.
thời gian. tzname[]Nếu là
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, ngược lại trả về
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]02 hoặc đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc một đối tượng chuỗi
Examples of Usage.
Ví dụ làm việc với đối tượng
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:007
Objects
class datetime. tzinfoĐây là một lớp cơ sở trừu tượng, có nghĩa là lớp này không được khởi tạo trực tiếp. Xác định một lớp con của để nắm bắt thông tin về một múi giờ cụ thể
Một thể hiện của [một lớp con cụ thể của] có thể được truyền cho các hàm tạo cho và các đối tượng. Các đối tượng thứ hai xem các thuộc tính của chúng là theo giờ địa phương và đối tượng hỗ trợ các phương thức hiển thị phần bù giờ địa phương từ UTC, tên của múi giờ và phần bù DST, tất cả đều liên quan đến đối tượng ngày hoặc giờ được truyền cho chúng
Bạn cần lấy được một lớp con cụ thể và [ít nhất] cung cấp các triển khai của các phương thức tiêu chuẩn cần thiết cho các phương thức bạn sử dụng. Mô-đun cung cấp , một phân lớp cụ thể đơn giản trong đó có thể biểu thị các múi giờ với phần bù cố định từ UTC, chẳng hạn như chính UTC hoặc EST và EDT của Bắc Mỹ
Yêu cầu đặc biệt đối với dưa chua. Một lớp con phải có một phương thức
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]18 có thể được gọi mà không có đối số, nếu không, nó có thể được chọn nhưng có thể không được giải nén lại. Đây là yêu cầu kỹ thuật có thể được nới lỏng trong tương lai
Một lớp con cụ thể của có thể cần phải thực hiện các phương pháp sau. Chính xác những phương pháp nào là cần thiết phụ thuộc vào việc sử dụng các đối tượng nhận biết. Nếu nghi ngờ, chỉ cần thực hiện tất cả chúng
tzinfo. utcoffset[dt]Trả về phần bù của giờ địa phương từ UTC, dưới dạng một đối tượng dương ở phía đông của UTC. Nếu giờ địa phương ở phía tây của UTC, điều này sẽ là âm
Điều này thể hiện tổng phần bù từ UTC; . Nếu không biết phần bù UTC, hãy trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03. Mặt khác, giá trị được trả về phải là một đối tượng nằm trong khoảng từ
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]26 đến
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]27 [độ lớn của phần bù phải nhỏ hơn một ngày]. Hầu hết các triển khai có thể sẽ giống như một trong hai
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:008
Nếu không trả lại
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03, thì cũng không nên trả lại
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03
Việc thực hiện tăng mặc định
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
tzinfo. dst[dt]Trả lại điều chỉnh thời gian tiết kiệm ánh sáng ban ngày [DST], dưới dạng đối tượng hoặc
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 nếu thông tin DST không được biết
Trả lại
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]83 nếu DST không có hiệu lực. Nếu DST có hiệu lực, hãy trả lại phần bù dưới dạng đối tượng [xem để biết chi tiết]. Lưu ý rằng phần bù DST, nếu có, đã được thêm vào phần bù UTC được trả về bởi , do đó, không cần tham khảo trừ khi bạn muốn nhận thông tin DST riêng. Ví dụ: gọi phương thức thuộc tính của nó để xác định cách đặt cờ
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False97 và gọi để giải thích cho các thay đổi DST khi vượt qua các múi giờ
Một thể hiện tz của một lớp con mô hình hóa cả thời gian tiêu chuẩn và thời gian ban ngày phải nhất quán theo nghĩa này
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]49
phải trả về cùng một kết quả cho mọi dt với
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]51 Đối với các lớp con lành mạnh, biểu thức này mang lại "độ lệch chuẩn" của múi giờ, không phụ thuộc vào ngày hoặc giờ mà chỉ phụ thuộc vào vị trí địa lý. Việc thực hiện ỷ lại vào việc này nhưng không phát hiện được vi phạm; . Nếu một lớp con không thể đảm bảo điều này, thì nó có thể ghi đè cài đặt mặc định của để hoạt động chính xác với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 bất kể
Hầu hết các triển khai có thể sẽ giống như một trong hai
>>> timedelta[hours=-5] datetime.timedelta[days=-1, seconds=68400] >>> print[_] -1 day, 19:00:009
hoặc là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False0
Việc thực hiện tăng mặc định
Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.
tzinfo. tzname[dt]Trả về tên múi giờ tương ứng với đối tượng dt, dưới dạng chuỗi. Không có gì về tên chuỗi được xác định bởi mô-đun và không có yêu cầu rằng nó có ý nghĩa cụ thể. Ví dụ: “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” đều là những câu trả lời hợp lệ. Trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 nếu không biết tên chuỗi. Lưu ý rằng đây chủ yếu là một phương thức chứ không phải là một chuỗi cố định bởi vì một số lớp con sẽ muốn trả về các tên khác nhau tùy thuộc vào giá trị cụ thể của dt được truyền, đặc biệt nếu lớp đó đang chiếm thời gian ban ngày
Việc thực hiện tăng mặc định
Các phương thức này được gọi bởi một hoặc đối tượng, để đáp lại các phương thức cùng tên của chúng. Một đối tượng chuyển chính nó làm đối số và một đối tượng chuyển
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 làm đối số. Do đó, các phương thức của lớp con nên được chuẩn bị để chấp nhận đối số dt của
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 hoặc của lớp
Khi
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 được thông qua, người thiết kế lớp sẽ quyết định câu trả lời tốt nhất. Ví dụ: trả về
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03 là phù hợp nếu lớp muốn nói rằng các đối tượng thời gian không tham gia vào các giao thức. Có thể hữu ích hơn khi
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]78 trả về phần bù UTC tiêu chuẩn, vì không có quy ước nào khác để khám phá phần bù tiêu chuẩn
When a object is passed in response to a method,
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]81 is the same object as self. methods can rely on this, unless user code calls methods directly. The intent is that the methods interpret dt as being in local time, and not need worry about objects in other timezones
There is one more method that a subclass may wish to override
tzinfo. fromutc[dt]This is called from the default implementation. When called from that,
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]81 is self, and dt’s date and time data are to be viewed as expressing a UTC time. The purpose of is to adjust the date and time data, returning an equivalent datetime in self’s local time
Most subclasses should be able to inherit the default implementation without problems. It’s strong enough to handle fixed-offset time zones, and time zones accounting for both standard and daylight time, and the latter even if the DST transition times differ in different years. An example of a time zone the default implementation may not handle correctly in all cases is one where the standard offset [from UTC] depends on the specific date and time passed, which can happen for political reasons. The default implementations of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 and may not produce the result you want if the result is one of the hours straddling the moment the standard offset changes
Skipping code for error cases, the default implementation acts like
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False1
In the following
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]95 file there are some examples of classes
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False2
Note that there are unavoidable subtleties twice per year in a subclass accounting for both standard and daylight time, at the DST transition points. For concreteness, consider US Eastern [UTC -0500], where EDT begins the minute after 1. 59 [EST] on the second Sunday in March, and ends the minute after 1. 59 [EDT] on the first Sunday in November
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False3
When DST starts [the “start” line], the local wall clock leaps from 1. 59 to 3. 00. A wall time of the form 2. MM doesn’t really make sense on that day, so
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]98 won’t deliver a result with
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]99 on the day DST begins. For example, at the Spring forward transition of 2016, we get
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False4
When DST ends [the “end” line], there’s a potentially worse problem. có một giờ không thể đánh vần rõ ràng theo giờ địa phương. the last hour of daylight time. In Eastern, that’s times of the form 5. MM UTC on the day daylight time ends. The local wall clock leaps from 1. 59 [daylight time] back to 1. 00 [standard time] again. Local times of the form 1. MM are ambiguous.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 mimics the local clock’s behavior by mapping two adjacent UTC hours into the same local hour then. In the Eastern example, UTC times of the form 5. MM and 6. MM both map to 1. MM when converted to Eastern, but earlier times have the attribute set to 0 and the later times have it set to 1. For example, at the Fall back transition of 2016, we get
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False5
Note that the instances that differ only by the value of the attribute are considered equal in comparisons
Applications that can’t bear wall-time ambiguities should explicitly check the value of the attribute or avoid using hybrid subclasses; there are no ambiguities when using , or any other fixed-offset subclass [such as a class representing only EST [fixed offset -5 hours], or only EDT [fixed offset -4 hours]]
Xem thêm
IANA timezone databaseThe module has a basic class [for handling arbitrary fixed offsets from UTC] and its attribute [a UTC timezone instance]
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'1 brings the IANA timezone database [also known as the Olson database] to Python, and its usage is recommended
The Time Zone Database [often called tz, tzdata or zoneinfo] contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules
Objects
The class is a subclass of , each instance of which represents a timezone defined by a fixed offset from UTC
Objects of this class cannot be used to represent timezone information in the locations where different offsets are used in different days of the year or where historical changes have been made to civil time
class datetime. timezone[offset , name=None]The offset argument must be specified as a object representing the difference between the local time and UTC. It must be strictly between
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]26 and
>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]27, otherwise is raised
The name argument is optional. If specified it must be a string that will be used as the value returned by the method
New in version 3. 2
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
timezone. utcoffset[dt]Return the fixed value specified when the instance is constructed
The dt argument is ignored. The return value is a instance equal to the difference between the local time and UTC
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
timezone. tzname[dt]Return the fixed value specified when the instance is constructed
If name is not provided in the constructor, the name returned by
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]24 is generated from the value of the
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]25 as follows. If offset is
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]83, the name is “UTC”, otherwise it is a string in the format
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]27, where ± is the sign of
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]25, HH and MM are two digits of
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]29 and
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]30 respectively
Changed in version 3. 6. Name generated from
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]31 is now plain
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]32, not
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]33. timezone. dst[dt]
Always returns
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]03timezone. fromutc[dt]
Return
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]35. The dt argument must be an aware instance, with
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 set to
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]38
Thuộc tính lớp
timezone. utcThe UTC timezone,
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]39
>>> from datetime import date
>>> date.fromisoformat['2019-12-04']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['20191204']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['2021-W01-1']
datetime.date[2021, 1, 4]
40 and >>> from datetime import date
>>> date.fromisoformat['2019-12-04']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['20191204']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['2021-W01-1']
datetime.date[2021, 1, 4]
41 Behavior
, , and objects all support a
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]45 method, to create a string representing the time under the control of an explicit format string
Conversely, the class method creates a object from a string representing a date and time and a corresponding format string
The table below provides a high-level comparison of
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]40 versus
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]41
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]50
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]51
Usage
Convert object to a string according to a given format
Parse a string into a object given a corresponding format
Type of method
Instance method
Class method
Method of
; ;
Signature
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]45
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]58
Mã định dạng >>> from datetime import date
>>> date.fromisoformat['2019-12-04']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['20191204']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['2021-W01-1']
datetime.date[2021, 1, 4]
40 và >>> from datetime import date
>>> date.fromisoformat['2019-12-04']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['20191204']
datetime.date[2019, 12, 4]
>>> date.fromisoformat['2021-W01-1']
datetime.date[2021, 1, 4]
41
Sau đây là danh sách tất cả các mã định dạng mà tiêu chuẩn C 1989 yêu cầu và chúng hoạt động trên tất cả các nền tảng có triển khai C tiêu chuẩn
chỉ thị
Nghĩa
Thí dụ
ghi chú
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]61
Ngày trong tuần là tên viết tắt của ngôn ngữ
CN, Mon, …, Sat [en_US];
So, Mo, …, Sa [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]62
Ngày trong tuần là tên đầy đủ của ngôn ngữ
Chủ Nhật, Thứ Hai, …, Thứ Bảy [en_US];
Sonntag, Montag, …, Samstag [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]63
Ngày trong tuần dưới dạng số thập phân, trong đó 0 là Chủ nhật và 6 là Thứ bảy
0, 1, …, 6
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]64
Ngày trong tháng dưới dạng số thập phân không đệm
01, 02, …, 31
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]65
Tháng là tên viết tắt của ngôn ngữ
Tháng 1, Tháng 2, …, Tháng 12 [en_US];
Tháng 1, Tháng 2, …, Dez [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]66
Tháng là tên đầy đủ của ngôn ngữ
Tháng Giêng, Tháng Hai, …, Tháng Mười Hai [en_US];
Januar, Februar, …, Tháng mười hai [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]67
Tháng dưới dạng số thập phân không đệm
01, 02, …, 12
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]68
Năm không có thế kỷ dưới dạng số thập phân không đệm
00, 01, …, 99
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]69
Năm với thế kỷ dưới dạng số thập phân
0001, 0002, …, 2013, 2014, …, 9998, 9999
[2]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]70
Giờ [đồng hồ 24 giờ] dưới dạng số thập phân không đệm
00, 01, …, 23
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]71
Giờ [đồng hồ 12 giờ] dưới dạng số thập phân không đệm
01, 02, …, 12
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]72
Ngôn ngữ tương đương với AM hoặc PM
AM, PM [en_US];
sáng, chiều [de_DE]
[1], [3]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]73
Phút dưới dạng số thập phân không đệm
00, 01, …, 59
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]74
Thứ hai dưới dạng số thập phân không đệm
00, 01, …, 59
[4], [9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]75
Micro giây dưới dạng số thập phân, được đệm bằng 0 thành 6 chữ số
000000, 000001, …, 999999
[5]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]76
Phần bù UTC ở dạng
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]77 [chuỗi trống nếu đối tượng là ngây thơ]
[trống], +0000, -0400, +1030, +063415, -030712. 345216
[6]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]78
Tên múi giờ [chuỗi trống nếu đối tượng ngây thơ]
[trống], UTC, GMT
[6]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]79
Ngày trong năm dưới dạng số thập phân không đệm
001, 002, …, 366
[9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]80
Số tuần của năm [Chủ nhật là ngày đầu tiên của tuần] dưới dạng số thập phân không đệm. Tất cả các ngày trong một năm mới trước Chủ nhật đầu tiên được coi là trong tuần 0
00, 01, …, 53
[7], [9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]81
Số tuần của năm [Thứ Hai là ngày đầu tiên của tuần] dưới dạng số thập phân không đệm. Tất cả các ngày trong năm mới trước ngày thứ Hai đầu tiên được coi là trong tuần 0
00, 01, …, 53
[7], [9]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]82
Đại diện ngày và giờ thích hợp của ngôn ngữ
Thứ ba 16 tháng 8 21. 30. 00 1988 [vi_Hoa Kỳ];
Di 16 Thg8 21. 30. 00 1988 [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]83
Đại diện ngày thích hợp của địa phương
16/08/88 [Không có];
16/08/1988 [en_Mỹ];
16. 08. 1988 [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]84
Đại diện thời gian thích hợp của địa phương
21. 30. 00 [vi_US];
21. 30. 00 [de_DE]
[1]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]85
Một ký tự
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]86 theo nghĩa đen
%
Một số chỉ thị bổ sung không bắt buộc theo tiêu chuẩn C89 được bao gồm để thuận tiện. Tất cả các tham số này đều tương ứng với các giá trị ngày theo tiêu chuẩn ISO 8601
chỉ thị
Nghĩa
Thí dụ
ghi chú
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]87
Năm ISO 8601 với thế kỷ đại diện cho năm chứa phần lớn tuần ISO [
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]88]
0001, 0002, …, 2013, 2014, …, 9998, 9999
[số 8]
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]89
ISO 8601 ngày trong tuần dưới dạng số thập phân trong đó 1 là Thứ Hai
1, 2, …, 7
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]88
ISO 8601 tuần dưới dạng số thập phân với Thứ Hai là ngày đầu tuần. Tuần 01 là tuần có ngày 04/01
01, 02, …, 53
[8], [9]
Những thứ này có thể không khả dụng trên tất cả các nền tảng khi được sử dụng với phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]40. Chỉ thị năm và tuần ISO 8601 không thể hoán đổi cho nhau với chỉ thị số năm và tuần ở trên. Gọi
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]41 với các chỉ thị ISO 8601 không đầy đủ hoặc mơ hồ sẽ làm tăng
Bộ mã định dạng đầy đủ được hỗ trợ khác nhau giữa các nền tảng, bởi vì Python gọi hàm
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]40 của thư viện C và các biến thể nền tảng là phổ biến. Để xem toàn 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 thông số định dạng không được hỗ trợ
Mới trong phiên bản 3. 6. _______75_______87,
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]89 và
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]88 đã được thêm vào.
Chi tiết kỹ thuật
Nói rộng ra,
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]98 hoạt động giống như mô-đun
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]00 mặc dù không phải tất cả các đối tượng đều hỗ trợ phương thức
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]42
Đối với phương thức của lớp, giá trị mặc định là
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]03. bất kỳ thành phần nào không được chỉ định trong chuỗi định dạng sẽ được lấy từ giá trị mặc định.
Sử dụng
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]04 tương đương với
>>> from datetime import timedelta >>> d = timedelta[microseconds=-1] >>> [d.days, d.seconds, d.microseconds] [-1, 86399, 999999]1
trừ khi định dạng bao gồm các thành phần giây phụ hoặc thông tin bù múi giờ, được hỗ trợ trong
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]05 nhưng bị loại bỏ bởi
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]06
Đối với các đối tượng, không nên sử dụng các mã định dạng cho năm, tháng và ngày vì các đối tượng không có các giá trị đó. Nếu chúng vẫn được sử dụng, thì
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]09 được thay thế cho năm và
>>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta[days=365] >>> another_year = timedelta[weeks=40, days=84, hours=23, .. minutes=50, seconds=600] >>> year == another_year True >>> year.total_seconds[] 31536000.05 cho tháng và ngày
Đối với các đối tượng, không nên sử dụng mã định dạng cho giờ, phút, giây và micro giây vì các đối tượng không có các giá trị như vậy. Nếu chúng vẫn được sử dụng,
>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]14 được thay thế cho chúng
Vì lý do tương tự, việc xử lý các chuỗi định dạng chứa các điểm mã Unicode không thể được biểu thị trong bộ ký tự của ngôn ngữ hiện tại cũng phụ thuộc vào nền tảng. Trên một số nền tảng, các điểm mã như vậy được giữ nguyên trong đầu ra, trong khi trên các nền tảng khác, thay vào đó,
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]50 có thể tăng hoặc trả về một chuỗi trống
ghi chú
Vì định dạng phụ thuộc vào ngôn ngữ hiện tại nên cần cẩn thận khi đưa ra các giả định về giá trị đầu ra. Thứ tự trường sẽ thay đổi [ví dụ: “tháng/ngày/năm” so với “ngày/tháng/năm”] và đầu ra có thể chứa các ký tự Unicode được mã hóa bằng cách sử dụng mã hóa mặc định của ngôn ngữ [ví dụ: nếu ngôn ngữ hiện tại là
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
16,Phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41 có thể phân tích các năm trong phạm vi [1, 9999] đầy đủ, nhưng các năm < 1000 phải được điền bằng 0 để có chiều rộng 4 chữ sốĐã thay đổi trong phiên bản 3. 2. Trong các phiên bản trước, phương thức
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
40 bị giới hạn trong các năm >= 1900.Đã thay đổi trong phiên bản 3. 3. Trong phiên bản 3. 2, phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
40 bị giới hạn trong các năm >= 1000.Khi được sử dụng với phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41, chỉ thị>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
72 chỉ ảnh hưởng đến trường giờ đầu ra nếu chỉ thị>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
71 được sử dụng để phân tích cú pháp giờKhông giống như mô-đun, mô-đun không hỗ trợ giây nhảy vọt
Khi được sử dụng với phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41, chỉ thị>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
75 chấp nhận từ một đến sáu chữ số và số 0 ở bên phải.>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
75 là một phần mở rộng cho tập hợp các ký tự định dạng trong tiêu chuẩn C [nhưng được triển khai riêng trong các đối tượng ngày giờ và do đó luôn có sẵn]Đối với một đối tượng ngây thơ, mã định dạng
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
76 và>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
78 được thay thế bằng các chuỗi rỗngĐối với một đối tượng nhận thức
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
76>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 được chuyển đổi thành một chuỗi có dạng>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
77, trong đó>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
67 là chuỗi 2 chữ số biểu thị số giờ bù giờ UTC,>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
38 là chuỗi 2 chữ số biểu thị số phút bù giờ UTC,>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
39 là chuỗi 2 chữ số biểu thị . Phần>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
40 bị bỏ qua khi phần bù là một số nguyên giây và cả phần>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
40 và phần>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
39 đều bị bỏ qua khi phần bù là một số nguyên phút. Ví dụ: nếu>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 trả về>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
45, thì>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
76 được thay thế bằng chuỗi>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
47Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
Đã thay đổi trong phiên bản 3. 7. Khi chỉ thị
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
76 được cung cấp cho phương thức>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41, phần bù UTC có thể có dấu hai chấm làm dấu phân cách giữa giờ, phút và giây. Ví dụ:>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
50 sẽ được phân tích thành phần bù của một giờ. Ngoài ra, cung cấp>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
51 giống hệt với>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
52.>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
78Trong
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
40,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
78 được thay thế bằng một chuỗi rỗng nếu>>> from datetime import timedelta >>> year = timedelta[days=365] >>> ten_years = 10 * year >>> ten_years datetime.timedelta[days=3650] >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta[days=3285] >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 [datetime.timedelta[days=1095], 3]
65 trả về>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
03;>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41 chỉ chấp nhận các giá trị nhất định cho>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
78bất kỳ giá trị nào trong
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
61 cho ngôn ngữ máy của bạncác giá trị mã hóa cứng
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
62 và>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
63
Vì vậy, một người nào đó sống ở Nhật Bản có thể có các giá trị hợp lệ là
>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
64,>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
62 và>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
63, nhưng có lẽ không phải là>>> from datetime import date >>> d = date[2002, 12, 31] >>> d.replace[day=26] datetime.date[2002, 12, 26]
67. Nó sẽ tăng>>> from datetime import timedelta >>> delta = timedelta[ .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ] >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta[days=64, seconds=29156, microseconds=10]
93 cho các giá trị không hợp lệĐã thay đổi trong phiên bản 3. 2. Khi chỉ thị
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
76 được cung cấp cho phương thức>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41, một đối tượng nhận biết sẽ được tạo ra.>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
4 của kết quả sẽ được đặt thành một thể hiện.Khi được sử dụng với phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
80 và>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
81 chỉ được sử dụng trong các phép tính khi ngày trong tuần và năm dương lịch [>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
69] được chỉ địnhTương tự như
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
80 và>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
81,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
88 chỉ được sử dụng trong tính toán khi ngày trong tuần và năm ISO [>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
87] được chỉ định trong chuỗi định dạng>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41. Cũng lưu ý rằng>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
87 và>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
69 không thể hoán đổi cho nhauKhi được sử dụng với phương pháp
>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
41, số 0 đứng đầu là tùy chọn đối với các định dạng>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
64,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
67,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
70,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
71,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
73,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
74,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
79,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
80,>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
81 và>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
88. Định dạng>>> from datetime import date >>> date.fromisoformat['2019-12-04'] datetime.date[2019, 12, 4] >>> date.fromisoformat['20191204'] datetime.date[2019, 12, 4] >>> date.fromisoformat['2021-W01-1'] datetime.date[2021, 1, 4]
68 không yêu cầu số 0 đứng đầu
chú thích
Nghĩa là, nếu chúng ta bỏ qua các tác động của Thuyết tương đối
Điều này phù hợp với định nghĩa của lịch “proleptic Gregorian” trong cuốn sách Tính toán Lịch của Dershowitz và Reingold, trong đó nó là lịch cơ sở cho tất cả các phép tính. Xem sách để biết các thuật toán chuyển đổi giữa các thứ tự Gregorian proleptic và nhiều hệ thống lịch khác