In this article, we will discuss various ways to convert time in seconds to H M S format in python. H M S format means Hours. Minutes. Seconds
Using arithmetic operations [Naïve method]
In this method, we use mathematic calculations to convert time in seconds to h m s format. Here we take the input from the user in seconds and convert it into the required format
Example
In this example, we convert a time in seconds to h m s format
seconds = int[input["Enter the number of seconds:"]] seconds = seconds % [24 * 3600] hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 print["%d:%02d:%02d" % [hour, minutes, seconds]]
Output
The output of the code is as follows
Enter the number of seconds:5000000 20:53:20
Using timedelta[] method
In this method, we use the . timedelta[] method to convert a time in seconds to h m s format. The . timedelta[] method is provided by the datetime library which is generally used to calculate the difference between given dates. We also this method for manipulating dates in python
Syntax
The syntax of the timedelta[] method is as follows
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
Here only send the seconds parameter as we need to convert time in seconds to h m s format
Example
Sau đây là mã ví dụ để chuyển đổi thời gian tính bằng giây sang định dạng H M S
import datetime timedelta_obj = datetime.timedelta[seconds=7896354] print["Time in H M S format: ",timedelta_obj]
Output
Đầu ra của đoạn mã trên như sau
Time in H M S format: 91 days, 9:25:54
sử dụng. phương thức strftime[]
Phương thức strftime[] được cung cấp bởi mô-đun datetime trong python. Ở đây chúng ta sử dụng phương thức strftime[] để chuyển đổi một chuỗi datetime thành datetime. Nó cũng được sử dụng để chuyển đổi datetime sang epoch
Epoch là điểm bắt đầu của thời gian và phụ thuộc vào nền tảng. Kỷ nguyên là ngày 1 tháng 1 năm 1970, 00. 00. 00 [UTC] trên Windows và hầu hết các hệ thống Unix và giây nhuận không được tính vào thời gian tính bằng giây kể từ kỷ nguyên. chúng tôi sử dụng thời gian. gmtime[0] để lấy kỷ nguyên trên một nền tảng nhất định
Syntax
Cú pháp của phương thức strftime[] được đưa ra dưới đây
date.strftime[format]
Trong đó, định dạng được sử dụng để chỉ định định dạng bắt buộc của đầu ra
Ở đây, chúng tôi muốn chuyển đổi thời gian tính bằng giây thành H. m. định dạng S. Vì vậy, mã định dạng, trong trường hợp này, là “%H. %M. %S"
Example
Sau đây là mã ví dụ để chuyển đổi thời gian tính bằng giây thành H. M. S trong python bằng cách sử dụng. phương thức strftime[]
Trong bài viết này, chúng ta sẽ chuyển đổi chuỗi DateTime thành %Y-%m-%d-%H. %M. định dạng %S. Đối với tác vụ này, chức năng strptime[] và strftime[] được sử dụng
strptime[] dùng để chuyển đổi chuỗi DateTime thành DateTime theo định dạng năm tháng ngày giờ phút giây
cú pháp
ngày giờ. strptime[my_date, “%d-%b-%Y-%H. %M. %S"]
strftime[] được sử dụng để chuyển đổi DateTime thành định dạng dấu thời gian cần thiết
cú pháp
ngày giờ. strftime[“%Y-%m-%d-%H. %M. %S"]
Nơi đây,
- &Y có nghĩa là năm
- %m có nghĩa là tháng
- %d có nghĩa là ngày
- %H có nghĩa là giờ
- %M có nghĩa là phút
- %S có nghĩa là giây
Đầu tiên lấy dấu thời gian DateTime làm Chuỗi. Sau đó, chuyển đổi nó thành DateTime bằng cách sử dụng strptime[]. Bây giờ, chuyển đổi sang định dạng DateTime cần thiết bằng cách sử dụng strftime
Cách tiếp cận #4. Thời gian sử dụng. thời gian strftime[]. strftime[] cho phép kiểm soát nhiều hơn đối với định dạng. Hình thức và thời gian. gmtime[] được truyền dưới dạng đối số. gmtime được sử dụng để chuyển đổi giây sang định dạng tuple đặc biệt mà strftime[] yêu cầu.Mặc dù số học ngày và giờ được hỗ trợ, trọng tâm của việc triển khai là trích xuất thuộc tính hiệu quả để định dạng và thao tác đầu ra
Xem thêm
Mô-đun>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False9
Các chức năng liên quan đến lịch chung
Mô-đun>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0
Thời gian truy cập và chuyển đổi
Mô-đun>>> 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
Múi giờ cụ thể đại diện cho cơ sở dữ liệu múi giờ IANA
ngày đóng góiThư viện của bên thứ ba với múi giờ mở rộng và hỗ trợ phân tích cú pháp
Đối tượng nhận thức và ngây thơ¶
Các đối tượng ngày và giờ có thể được phân loại là "nhận biết" hoặc "ngây thơ" tùy thuộc vào việc chúng có bao gồm thông tin múi giờ hay không
Với kiến thức đầy đủ về các điều chỉnh thời gian chính trị và thuật toán hiện hành, chẳng hạn như múi giờ và thông tin về thời gian tiết kiệm ánh sáng ban ngày, một đối tượng nhận biết có thể định vị chính nó so với các đối tượng nhận biết khác. Một đối tượng nhận thức đại diện cho một thời điểm cụ thể không thể giải thích được. 1
Một đối tượng ngây thơ không chứa đủ thông tin để định vị chính nó một cách rõ ràng so với các đối tượng ngày/giờ khác. Việc một đối tượng ngây thơ đại diện cho Giờ phối hợp quốc tế [UTC], giờ địa phương hay thời gian ở một số múi giờ khác hoàn toàn phụ thuộc vào chương trình, giống như việc một số cụ thể đại diện cho mét, dặm hay khối lượng hay không là tùy thuộc vào chương trình. Các đối tượng ngây thơ dễ hiểu và dễ làm việc với cái giá phải trả là bỏ qua một số khía cạnh của thực tế
Đối với các ứng dụng yêu cầu các đối tượng nhận biết, 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 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 có thuộc tính thông tin múi giờ tùy chọn,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4, có thể được đặt thành một thể hiện của một lớp con của lớp trừu tượng
>>> 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ác đối tượ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'4 này nắm bắt thông tin về độ lệch so với thời gian UTC, tên múi giờ và liệu thời gian tiết kiệm ánh sáng ban ngày có hiệu lực hay không
Chỉ có một lớp
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 cụ thể, lớ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'8, được cung cấp bởi mô-đun
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7. Lớ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'8 có thể đại diện cho các múi giờ đơn giản với độ lệch cố định từ UTC, chẳng hạn như chính UTC hoặc các múi giờ EST và EDT của Bắc Mỹ. Hỗ trợ các múi giờ ở mức độ chi tiết sâu hơn tùy thuộc vào ứng dụng. Các quy tắc điều chỉnh thời gian trên toàn thế giới mang tính chính trị hơn là hợp lý, thay đổi thường xuyên và không có tiêu chuẩn nào phù hợp cho mọi ứng dụng ngoài UTC
Hằng số¶
Mô-đun
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 xuất các hằng số saungày giờ. THÁNG NĂM ¶
Số năm nhỏ nhất được phép trong một đối tượng
Enter the number of seconds:5000000 20:53:2092 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7.
Enter the number of seconds:5000000 20:53:2094 là
Enter the number of seconds:5000000 20:53:2095ngày giờ. MAX NĂM ¶
Số năm lớn nhất được phép trong một đối tượng
Enter the number of seconds:5000000 20:53:2092 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7.
Enter the number of seconds:5000000 20:53:2098 là
Enter the number of seconds:5000000 20:53:2099ngày giờ. UTC ¶
Bí danh cho múi giờ UTC đơn lẻ
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]70
Mới trong phiên bản 3. 11
Các loại có sẵn¶
lớp ngày giờ. ngàyMột ngày ngây thơ được lý tưởng hóa, giả sử rằng lịch Gregorian hiện tại luôn có hiệu lực. Thuộc tính.
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]71,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]72 và
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]73lớp ngày giờ. thời gian
Thời gian lý tưởng hóa, không phụ thuộc vào bất kỳ ngày cụ thể nào, giả sử rằng mỗi ngày có chính xác 24*60*60 giây. [Không có khái niệm “giây nhuận” ở đây. ] Thuộc tính.
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]74,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]75,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]76,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]77 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4lớp ngày giờ. ngày giờ
Một sự kết hợp của một ngày và một thời gian. Thuộc tính.
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]71,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]72,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]73,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]74,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]75,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]76,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]77 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4lớp ngày giờ. đồng hồ thời gian
Khoảng thời gian thể hiện sự khác biệt giữa hai phiên bản
Enter the number of seconds:5000000 20:53:2092,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 đối với độ phân giải micro giâylớp ngày giờ. tzinfo
Một lớp cơ sở trừu tượng cho các đối tượng thông tin múi giờ. Chúng được sử dụng bởi các lớp
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 để cung cấp khái niệm điều chỉnh thời gian có thể tùy chỉnh [ví dụ: để tính múi giờ và/hoặc thời gian tiết kiệm ánh sáng ban ngày]lớp ngày giờ. múi giờ
Một lớp triển khai lớp cơ sở trừu tượng
>>> 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 dưới dạng phần bù cố định từ UTC
Mới trong phiên bản 3. 2
Các đối tượng của các loại này là bất biến
quan hệ phân lớp
date.strftime[format]0
Tài sản chung¶
Các loại
Enter the number of seconds:5000000 20:53:2092,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8 có chung những đặc điểm này
Các đối tượng của các loại này là bất biến
Các đối tượng thuộc loại này có thể băm được, nghĩa là chúng có thể được sử dụng làm khóa từ điển
Các đối tượng thuộc loại này hỗ trợ tẩy hiệu quả thông qua mô-đun
date.strftime[format]
57
Xác định xem một đối tượng là Aware hay Naive¶
Các đối tượng thuộc loại
Enter the number of seconds:5000000 20:53:2092 luôn ngây thơ
Một đối tượng thuộc loạ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'0 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 có thể nhận thức hoặc ngây thơ
Một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 d nhận biết nếu cả hai điều sau đây đều đúng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
002 không phải là>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
004 không trả lại>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003
Mặt khác, d là ngây thơ
Một đối tượ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'0 t nhận biết được nếu cả hai điều sau đây đều đúng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
007 không phải là>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
009 không trả lại>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003
Nếu không, t là ngây thơ
Sự khác biệt giữa nhận thức và ngây thơ không áp dụng cho các đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
011 Đối tượng¶
Một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 đạ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 gianlớ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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False014. 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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
015>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
016 [số giây trong một ngày]>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
017
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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False0
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, thì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 sẽ 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ụ
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8
Thuộc tính lớp
đồng bằng thời gian. phút ¶Đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 tiêu cực nhất,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False020đồng bằng thời gian. tối đa ¶
Đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 tích cực nhất,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False022đồ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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 không bằng nhau,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False024
Lưu ý rằng, do chuẩn hóa, ________ 6025 > ________ 6026.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False027 không thể biểu diễn dưới dạng đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011
Thuộc tính phiên bản [chỉ đọc]
Thuộc tính
Giá trị
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False029
Giữa -999999999 và bao gồm cả 999999999
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False030
Bao gồm từ 0 đến 86399
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False031
Bao gồm từ 0 đến 999999
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 False032
Tổng của t2 và t3. Sau đó t1-t2 == t3 và t1-t3 == t2 là đúng. [1]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False033
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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False034
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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False035
Nói chung, t1 * i == t1 * [i-1] + t1 là đúng. [1]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False036
Delta nhân với một số float. Kết quả được làm tròn đến bội số gần nhất của timedelta. độ phân giải sử dụng nửa vòng đến chẵn
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False037
Chia [3] tổng thời lượng t2 cho đơn vị khoảng thời gian t3. Trả về một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False038
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False039
Delta chia cho float hoặc int. Kết quả được làm tròn đến bội số gần nhất của timedelta. độ phân giải sử dụng nửa vòng đến chẵn
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False040 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False041
Sàn được tính và phần còn lại [nếu có] bị vứt đi. Trong trường hợp thứ hai, một số nguyên được trả về. [3]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False042
Phần còn lại được tính như một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011. [3]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False044
Tính thương và số dư.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False045 [3] và
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False046. q là một số nguyên và r là một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False048
Trả về một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 có cùng giá trị. [2]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False050
tương đương với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011[-t1. ngày, -t1. giây, -t1. micro giây] và đến t1* -1. [1][4]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False052
tương đương với +t khi
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False053 và -t khi
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False054. [2]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False055
Trả về một chuỗi ở dạng ________ 6056, trong đó D là âm cho âm ________ 6057. [5]
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False058
Trả về một biểu diễn chuỗi của đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 dưới dạng lệnh gọi hàm tạo với các giá trị thuộc tính chính tắc
ghi chú
Điều này là chính xác nhưng có thể tràn
Điều này là chính xác và không thể tràn
Chia cho 0 tăng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
060-timedelta. max không thể biểu diễn dưới dạng đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
011Biểu diễn chuỗi của đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
011 đượ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ụEnter the number of seconds:5000000 20:53:20
23Biểu thức
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
063 sẽ luôn bằng biểu thức>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
064 trừ khi t3 bằng>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
025;
Ngoài các hoạt động được liệt kê ở trên, các đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 hỗ trợ một số phép cộng và phép trừ với các đối tượng
Enter the number of seconds:5000000 20:53:2092 và
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 [xem bên dưới]
Đã thay đổi trong phiên bản 3. 2. Phép chia tầng và phép chia thực sự của một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 cho một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 khác hiện được hỗ trợ, cũng như các phép toán còn lại và hàm
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False071. Phép chia và phép nhân thực sự của đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 với đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False038 hiện đã được hỗ trợ.
So sánh các đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 được hỗ trợ, với một số lưu ý
Các phép so sánh
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False075 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False076 luôn trả về một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False077, 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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False078 và
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False079], khi một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 được so sánh với một đối tượng thuộc loại khác, thì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False081 đượ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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 được coi là đúng khi và chỉ khi nó không bằng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False083
Phương thức sơ thẩm
đồng bằng thời gian. total_seconds[] ¶Trả về tổng số giây có trong khoảng thời gian. Tương đương với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False084. Đối với các đơn vị khoảng thời gian khác với giây, hãy sử dụng biểu mẫu chia trực tiếp [e. g.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False085]
Lưu ý rằng trong khoảng thời gian rất lớn [hơn 270 năm trên hầu hết các nền tảng], phương pháp này sẽ mất độ chính xác micro giây
Mới trong phiên bản 3. 2
Ví dụ về cách sử dụng. >>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
011¶
Một ví dụ bổ sung về chuẩn hóa
Enter the number of seconds:5000000 20:53:209
Ví dụ về số học
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]7
Enter the number of seconds:5000000
20:53:20
92 Đối tượng¶
Một đối tượng
Enter the number of seconds:5000000 20:53:2092 đạ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
January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on. 2
class datetime. date[year , month , day] ¶All arguments are required. Arguments must be integers, in the following ranges
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
090>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
091>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
092
If an argument outside those ranges is given,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 is raised
Other constructors, all class methods
classmethod date. today[] ¶Return the current local date
This is equivalent to
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False094classmethod date. fromtimestamp[timestamp] ¶
Return the local date corresponding to the POSIX timestamp, such as is returned by
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False095
This may raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018, if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 function, and
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 on
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'800
Changed in version 3. 3. Raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 instead of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 function. Raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 instead of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 on
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 failure. classmethod date. fromordinal[ordinal] ¶
Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 is raised unless
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'808. For any date d,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'809classmethod date. fromisoformat[date_string] ¶
Return a
Enter the number of seconds:5000000 20:53:2092 corresponding to a date_string given in any valid ISO 8601 format, except ordinal dates [e. g.
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'811]
Enter the number of seconds:5000000 20:53:2077
New in version 3. 7
Changed in version 3. 11. Previously, this method only supported the format
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'812. classmethod date. fromisocalendar[year , week , day] ¶
Return a
Enter the number of seconds:5000000 20:53:2092 corresponding to the ISO calendar date specified by year, week and day. This is the inverse of the function
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'814
New in version 3. 8
Thuộc tính lớp
date. min ¶The earliest representable date,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'815date. max ¶
The latest representable date,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'816date. resolution ¶
Sự khác biệt nhỏ nhất có thể giữa các đối tượng ngày không bằng nhau,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'817
Thuộc tính phiên bản [chỉ đọc]
date. year ¶Between
Enter the number of seconds:5000000 20:53:2094 and
Enter the number of seconds:5000000 20:53:2098 inclusivedate. month ¶
Between 1 and 12 inclusive
date. day ¶Between 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ả
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'820
date2 will be
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'821 days after date1. [1]
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'822
Computes date2 such that
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'823. [2]
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'824
[3]
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'825
date1 is considered less than date2 when date1 precedes date2 in time. [4]
ghi chú
date2 is moved forward in time if
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
826, or backward if>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
827. Afterward>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
828.>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
829 and>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
830 are ignored.>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
018 is raised if>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
832 would be smaller thanEnter the number of seconds:5000000 20:53:20
94 or larger thanEnter the number of seconds:5000000 20:53:20
98>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
829 and>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
830 are ignoredThis is exact, and cannot overflow. timedelta. seconds and timedelta. microseconds are 0, and date2 + timedelta == date1 after
In other words,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
825 if and only if>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
838. Date comparison raises>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 if the other comparand isn’t also aEnter the number of seconds:5000000 20:53:20
92 object. However,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
841 is returned instead if the other comparand has 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'
842 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when aEnter the number of seconds:5000000 20:53:20
92 object is compared to an object of a different type,>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 is raised unless the comparison is>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
075 or>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
076. The latter cases return>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
847 or>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
848, respectively
In Boolean contexts, all
Enter the number of seconds:5000000 20:53:2092 objects are considered to be true
Phương thức sơ thẩm
ngày. thay thế[năm=chính mình. năm , tháng=mình. tháng , ngày=chính mình. ngày] ¶Trả về một ngày có cùng giá trị, ngoại trừ các tham số được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định
Thí dụ
date.strftime[format]5ngày. thời gian[] ¶
Trả lại một
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'850 chẳng hạn như trả lại bở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'851
Giờ, phút và giây là 0 và cờ DST là -1
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'852 tương đương với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False00
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'853 là số ngày trong năm hiện tại bắt đầu bằng
Enter the number of seconds:5000000 20:53:2095 cho ngày 1 tháng 1ngày. bình thường[] ¶
Trả về thứ tự Gregorian proleptic của ngày, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1. Đối với bất kỳ đối tượng
Enter the number of seconds:5000000 20:53:2092 d,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'809ngày. weekday[] ¶
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'857, a Wednesday. See also
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'858date. isoweekday[] ¶
Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'859, a Wednesday. See also
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'860,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'861ngày. isocalendar[] ¶
Return a named tuple object with three components.
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]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'863 and
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'864.
The ISO calendar is a widely used variant of the Gregorian calendar. 3
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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False01
Changed in version 3. 9. Result changed from a tuple to a named tuple .
date. isoformat[] ¶Return a string representing the date in ISO 8601 format,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'812
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False02date. __str__[] ¶
For a date d,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'866 is equivalent to
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'867date. ctime[] ¶
Return a string representing the date
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False03
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'868 is equivalent to
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False04
on platforms where the native 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'869 function [which
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'870 invokes, but which
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'871 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 strftime[] and strptime[] Behavior .
date. __format__[format] ¶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'872. This makes it possible to specify a format string for a
Enter the number of seconds:5000000 20:53:2092 object in formatted string literals and when using
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'874. For a complete list of formatting directives, see strftime[] and strptime[] Behavior .
Examples of Usage. Enter the number of seconds:5000000
20:53:20
92¶
Example of counting days to an event
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False05
Thêm ví dụ về cách làm việc với
Enter the number of seconds:5000000 20:53:2092
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False06
>>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 Objects¶
A
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object is a single object containing all the information from a
Enter the number of seconds:5000000 20:53:2092 object and 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'0 object
Like a
Enter the number of seconds:5000000 20:53:2092 object,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 assumes the current Gregorian calendar extended in both directions; like 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'0 object,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, or an instance of 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'4 subclass. The remaining arguments must be integers in the following ranges
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
090,>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
091,>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
092,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
890,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
891,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
892,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
893,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
894
If an argument outside those ranges is given,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 is raised
New in version 3. 6. Added the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 argument.
Other constructors, all class methods
classmethod datetime. today[] ¶Return the current local datetime, 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003
Equivalent to
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False07
See also
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'899,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'800
This method is functionally equivalent to
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'899, but without a
Enter the number of seconds:5000000 20:53:202302 parameterclassmethod datetime. now[tz=None] ¶
Return the current local date and time
If optional argument tz is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 or not specified, this is like
Enter the number of seconds:5000000 20:53:202304, but, if possible, supplies more precision than can be gotten from going through a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False095 timestamp [for example, this may be possible on platforms supplying the C
Enter the number of seconds:5000000 20:53:202306 function]
If tz is not
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, it must be an instance of 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'4 subclass, and the current date and time are converted to tz’s time zone
This function is preferred over
Enter the number of seconds:5000000 20:53:202304 and
Enter the number of seconds:5000000 20:53:202310classmethod datetime. utcnow[] ¶
Return the current UTC date and time, 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003
This is like
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'899, but returns the current UTC date and time, as a naive
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object. An aware current UTC datetime can be obtained by calling
Enter the number of seconds:5000000 20:53:202315. See also
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'899
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
Enter the number of seconds:5000000 20:53:202315classmethod datetime. fromtimestamp[timestamp , tz=None] ¶
Return the local date and time corresponding to the POSIX timestamp, such as is returned by
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False095. Nếu đối số tùy chọn tz là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc không được chỉ định, thì dấu thời gian được chuyển đổi thành ngày giờ địa phương của nền tảng và đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 được trả về là ngây thơ
Nếu tz không phải là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, thì nó phải là một thể hiện của lớp con
>>> 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 và dấu thời gian được chuyển đổi thành múi giờ của tz
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'800 may raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018, if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 or
Enter the number of seconds:5000000 20:53:202328 functions, and
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 on
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 or
Enter the number of seconds:5000000 20:53:202328 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'800, and then it’s possible to have two timestamps differing by a second that yield identical
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 objects. This method is preferred over
Enter the number of seconds:5000000 20:53:202334
Changed in version 3. 3. Raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 instead of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 if the timestamp is out of the range of values supported by the platform C
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 or
Enter the number of seconds:5000000 20:53:202328 functions. Raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 instead of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 on
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False097 or
Enter the number of seconds:5000000 20:53:202328 failure.
Đã thay đổi trong phiên bản 3. 6. ______7800 có thể trả về các phiên bản 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'896 được đặt thành 1. phương thức lớp ngày giờ. utcfromtimestamp[dấu thời gian] ¶
Trả về UTC
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 tương ứng với dấu thời gian POSIX, 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'4
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003. [Đối tượng kết quả là ngây thơ. ]
Điều này có thể tăng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018, nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm
Enter the number of seconds:5000000 20:53:202328 của nền tảng C và
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 khi lỗi
Enter the number of seconds:5000000 20:53:202328. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038
Để nhận biết một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7, hãy gọ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'800
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False08
Trên nền tảng tuân thủ POSIX, nó tương đương với biểu thức sau
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False09
ngoại trừ công thức sau luôn hỗ trợ phạm vi năm đầy đủ. giữa
Enter the number of seconds:5000000 20:53:2094 và bao gồm cả
Enter the number of seconds:5000000 20:53:2098
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 trong UTC. Do đó, cách được đề xuất để tạo một đối tượng đại diện cho một dấu thời gian cụ thể trong UTC là gọi số
Enter the number of seconds:5000000 20:53:202358
Đã thay đổi trong phiên bản 3. 3. Tăng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 thay vì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 nếu dấu thời gian nằm ngoài phạm vi giá trị được hàm
Enter the number of seconds:5000000 20:53:202328 nền tảng C hỗ trợ. Tăng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False098 thay vì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 khi lỗi
Enter the number of seconds:5000000 20:53:202328. phương thức lớp ngày giờ. từ thứ tự[thứ tự] ¶
Trả lại
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 tương ứng với thứ tự Gregorian proleptic, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 được nâng lên trừ khi
Enter the number of seconds:5000000 20:53:202367. Giờ, phút, giây và micro giây của kết quả đều là 0 và
>>> 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 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003phương thức lớp ngày giờ. kết hợp[ngày , thời gian . tzinfo, tzinfo=self.tzinfo] ¶
Trả về một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 mới có thành phần ngày bằng với đối tượng
Enter the number of seconds:5000000 20:53:2092 đã cho và thành phần thời gian của nó bằng với đối tượ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'0 đã cho. Nếu đối số tzinfo được cung cấp, giá trị của nó được sử dụng để đặt thuộc tí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'4 của kết quả, nếu không thì thuộc tí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'4 của đối số thời gian được sử dụng
Đối với bất kỳ đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 d,
Enter the number of seconds:5000000 20:53:202376. Nếu ngày là một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7, các thành phần thời gian và thuộc tí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'4 của nó sẽ bị bỏ qua
Đã thay đổi trong phiên bản 3. 6. Đã thêm đối số tzinfo.
phương thức lớp ngày giờ. fromisoformat[date_string] ¶Trả lại một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 tương ứng với một chuỗi ngày ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau
Độ lệch múi giờ có thể có giây phân số
Dấu phân cách
Enter the number of seconds:5000000 20:53:20
2380 có thể được thay thế bằng bất kỳ ký tự unicode nàoNgày thứ tự hiện không được hỗ trợ
Giờ và phút phân số không được hỗ trợ
ví dụ
>>> 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
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
Enter the number of seconds:5000000 20:53:202381 hoặc
Enter the number of seconds:5000000 20:53:202382. phương thức lớp ngày giờ. từ dương lịch[năm , tuần, day]¶
Trả lại một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 tương ứng với ngày theo lịch ISO được chỉ định theo năm, tuần và ngày. Các thành phần không phải ngày tháng của ngày giờ được điền bằng các giá trị mặc định bình thường của chúng. Đây là nghịch đảo của hàm
Enter the number of seconds:5000000 20:53:202384
New in version 3. 8
phương thức lớp ngày giờ. strptime[date_string , định dạng]¶Trả về một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 tương ứng với date_string, được phân tích cú pháp theo định dạng
Điều này tương đương với
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'81
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 được nâng lên nếu chuỗi ngày và định dạng không thể được phân tích cú pháp bởi
Enter the number of seconds:5000000 20:53:202387 hoặc nếu nó trả về một giá trị không phải là bộ thời gian. Để biết danh sách đầy đủ các lệnh định dạng, hãy xem strftime[] và strptime[] Hành vi .
Thuộc tính lớp
ngày giờ. phút ¶Đại diện sớm nhất
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7,
Enter the number of seconds:5000000 20:53:202389ngày giờ. tối đa ¶
Đại diện mới nhất
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7,
Enter the number of seconds:5000000 20:53:202391ngày giờ. độ phân giải ¶
Sự khác biệt nhỏ nhất có thể giữa 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 không bằng nhau,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False024
Thuộc tính phiên bản [chỉ đọc]
ngày giờ. năm ¶Between
Enter the number of seconds:5000000 20:53:2094 and
Enter the number of seconds:5000000 20:53:2098 inclusivengày giờ. tháng ¶
Between 1 and 12 inclusive
ngày giờ. ngày ¶Between 1 and the number of days in the given month of the given year
ngày giờ. giờ ¶Trong
Enter the number of seconds:5000000 20:53:202396ngày giờ. phút ¶
Trong
Enter the number of seconds:5000000 20:53:202397ngày giờ. giây ¶
Trong
Enter the number of seconds:5000000 20:53:202397ngày giờ. micro giây ¶
Trong
Enter the number of seconds:5000000 20:53:202399ngày giờ. tzinfo ¶
Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 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ồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] Giá trị 0 [1] thể hiện sớm hơn [muộn hơn] của hai thời điểm có cùng biểu diễn thời gian tường
Mới trong phiên bản 3. 6
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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 với
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7. [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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
821 > 0 hoặc lùi lại nếu>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
821 < 0. Kết quả có thuộc tí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'
4 giống như dữ liệu thời gian đầu vào và datetime2 - datetime1 == timedelta sau.>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
018 được nâng lên nếu datetime2. năm sẽ nhỏ hơnEnter the number of seconds:5000000 20:53:20
94 hoặc lớn hơnEnter the number of seconds:5000000 20:53:20
98. 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ó thuộc tí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'
4 giống như 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ếtPhép trừ của một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 từ một>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 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 biết và người kia ngây thơ,>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 được nâng lênNếu cả hai đều ngây thơ hoặc cả hai đều biết và có cùng thuộc tí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'
4, các thuộc tí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'
4 sẽ bị bỏ qua và kết quả là một đối tượng>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
011 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 biết và có các thuộc tí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'
4 khác nhau, thì>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
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 người so sánh ngây thơ và người kia nhận thức được, thì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 sẽ được nâng lên nếu cố gắng so sánh đơn hàng. Để 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ứcNếu cả hai đối tượng so sánh đều biết và có cùng thuộc tí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'
4, thì thuộc tính chung>>> 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 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>>> 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 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 nhận thức và ngây thơ không nâng cao>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081.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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 nếu đối tượng so sánh khác cũng không phải là đối tượng>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7. Tuy nhiên, thay vào đó,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
841 được trả về nếu đối tượng so sánh khác có thuộc tí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'
842. 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>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 được so sánh với một đối tượng thuộc loại khác,>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
081 được nâng lên trừ khi so sánh là>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
075 hoặc>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
076. Các trường hợp sau lần lượt trả về>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
847 hoặ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'
848
Phương thức sơ thẩm
ngày giờ. ngày[] ¶Trả về đối tượng
Enter the number of seconds:5000000 20:53:2092 có cùng năm, tháng và ngàyngày giờ. thời gian[] ¶
Trả về đối tượ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'0 có cùng giờ, phút, giây, micro giây và gấ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'4 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003. Xem thêm phương pháp
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False47
Đã 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 được trả về. ngày giờ. timetz[] ¶
Trả về đối tượ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'0 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False50
Đã 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 đượ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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 argument. ngày giờ. múi giờ[tz=Không có]¶
Trả về một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 với thuộc tí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'4 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 lớp con
>>> 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 và các phương thức
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 và
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 của nó không được trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003. 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ủ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'8 với 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à self. 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False70 có thể được ghi đè trong lớp con
>>> 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 để ảnh hưởng đến kết quả được trả về bởi
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72. Bỏ qua các trường hợp lỗi,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 hoạt độ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'82
Đã 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 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
>>> 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 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, 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 False78 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 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
>>> 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 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, 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 False84 và đưa ra một ngoại lệ nếu cái sau không trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc một đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 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
>>> 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 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, 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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc một đối tượng chuỗi,ngày giờ. thời gian[] ¶
Trả lại một
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'850 chẳng hạn như trả lại bở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'851
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'852 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'83
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'853 là số ngày trong năm hiện tại bắt đầu bằng
Enter the number of seconds:5000000 20:53:2095 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 đặt theo phương pháp
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58.
>>> 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 là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003,
>>> 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 thể hiện d là ngây thơ, thì điều này giố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'852 ngoại trừ việc
>>> 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à trả về một giá trị
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'850 cho thời gian chuẩn hóa.
>>> 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 một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 có thể được nâng lên nếu d. năm là
Enter the number of seconds:5000000 20:53:2094 hoặc
Enter the number of seconds:5000000 20:53:2098 và điều chỉnh UTC vượt 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 thức để biểu thị thời gian trong 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ụ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'25
Trả về thứ tự Gregorian proleptic của ngày. Giống như
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'26ngày giờ. dấu thời gian[] ¶
Trả về dấu thời gian POSIX tương ứng với phiên bản
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7. Giá trị trả về là một
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False038 tương tự như giá trị được trả về bởi
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False095
Các phiên bản
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ được giả định là đại diện cho giờ địa phương và phương pháp này dựa vào hàm C
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'31 của nền tảng để thực hiện chuyển đổi. Vì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 hỗ trợ phạm vi giá trị rộng hơn so 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'31 trên nhiều nền tảng nên phương pháp này có thể tăng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False018 cho những thời điểm xa trong quá khứ hoặc xa trong tương lai
Đối với các phiên bản
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 đã biết, giá trị trả về được tí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'84
Mới trong phiên bản 3. 3
Đã thay đổi trong phiên bản 3. 6. Phương thứ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'36 sử dụng thuộc tí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'896 để phân biệt thời gian trong một khoảng thời gian lặp lại.
Ghi chú
Không có phương pháp nào để lấy dấu thời gian POSIX trực tiếp từ một phiên bản
>>> 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 thời gian UTC. Nếu ứng dụng của bạn sử dụng quy ước này và múi giờ hệ thống của bạn không được đặt thành UTC, 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'85
hoặc bằng cách tính toán dấu thời gian trực tiế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'86ngà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ê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'858ngà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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'860,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'861ngày giờ. isocalendar[] ¶
Trả về một bộ có tên với ba thành phần.
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]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'863 và
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'864. Giống như
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'48. ngày giờ. isoformat[sep=', timespec='auto']¶
Trả về một chuỗi biểu thị ngày và giờ ở định dạng ISO 8601
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
49, nếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 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ếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 là 0
Nếu
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 không trả về
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, 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ếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 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ếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 là 0
ví dụ
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'87
Đối số tùy chọn sep [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'59] là dấu tách một ký tự, được đặt giữa phần ngày và giờ của kết quả. Ví dụ
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'88
Đố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ếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74 ở đị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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74 vàdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
75 ở đị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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74,datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
75 vàdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
76 ở đị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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 sẽ được nâng lên trên một đối số timespec không hợp 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'89
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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 d,
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'866 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ờ
Enter the number of seconds:5000000 20:53:20230
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ơ
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'868 is equivalent to
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False04
trên các nền tảng có chức năng 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'869 gốc [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'870 gọi, nhưng 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'88 không gọi] phù hợp với tiêu chuẩn Cngày giờ. thời gian strf[đị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 lệnh định dạng, hãy xem strftime[] và strptime[] Hành vi .
ngày giờ. __format__[định dạng] ¶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'89. Điều này giúp có thể chỉ định chuỗi định dạng cho đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 trong ký tự chuỗi được định dạng và khi 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'874. Để biết danh sách đầy đủ các lệnh định dạng, hãy xem strftime[] và strptime[] Hành vi .
Ví dụ về cách sử dụng. >>> from datetime import timedelta
>>> delta1 = timedelta[seconds=57]
>>> delta2 = timedelta[hours=25, seconds=2]
>>> delta2 != delta1
True
>>> delta2 == 5
False
7¶
Ví dụ làm việc với đối tượng
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7
Enter the number of seconds:5000000 20:53:20232
Ví dụ bên dưới định nghĩa một lớp con
>>> 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 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 đó
Enter the number of seconds:5000000 20:53:20233
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
Enter the number of seconds:5000000 20:53:20234
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
0 Đối tượng¶
Đối tượ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'0 đại diện cho thời gian trong ngày [cục bộ], không phụ thuộc vào bất kỳ ngày cụ thể nào và có thể điều chỉnh thông qua đối tượng
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4lớ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 >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 hoặc một thể hiện của lớp con
>>> 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ác đối số còn lại phải là số nguyên trong các phạm vi sau
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
890,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
891,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
892,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
893,>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
894
Nếu một đối số nằm ngoài các phạm vi đó được đưa ra, thì
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 được nêu ra. Tất cả mặc định là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False014 ngoại trừ tzinfo, mặc định là
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003
Thuộc tính lớp
thời gian. phút ¶Đại diện sớm nhất
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0,
Enter the number of seconds:5000000 20:53:20910thời gian. tối đa ¶
Đại diện mới nhất
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0,
Enter the number of seconds:5000000 20:53:20912thời gian. độ phân giải ¶
Sự khác biệt nhỏ nhất có thể giữa các đối tượ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'0 không bằng nhau,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False024, mặc dù lưu ý rằng số học trên các đối tượ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'0 không được hỗ trợ
Thuộc tính phiên bản [chỉ đọc]
thời gian. giờ ¶Trong
Enter the number of seconds:5000000 20:53:202396thời gian. phút ¶
Trong
Enter the number of seconds:5000000 20:53:202397thời gian. giây ¶
Trong
Enter the number of seconds:5000000 20:53:202397thời gian. micro giây ¶
Trong
Enter the number of seconds:5000000 20:53:202399thời gian. tzinfo ¶
Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 hoặc
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 nếu không có đối số nào được truyềntime. fold ¶
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ồ được lùi lại vào cuối giờ tiết kiệm ánh sáng ban ngày hoặc khi độ lệch UTC cho vùng hiện tại bị giảm vì lý do chính trị. ] Giá trị 0 [1] thể hiện sớm hơn [muộn hơn] của hai thời điểm có cùng biểu diễn thời gian tường
Mới trong phiên bản 3. 6
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 objects support comparison of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 to
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0, where a is considered less than b when a precedes b in time. If one comparand is naive and the other is aware,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False081 is raised if an order comparison is attempted. For equality comparisons, naive instances are never equal to aware instances
If both comparands are aware, and have the same
>>> 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 attribute, the common
>>> 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 attribute is ignored and the base times are compared. If both comparands are aware and have different
>>> 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 attributes, the comparands are first adjusted by subtracting their UTC offsets [obtained from
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False30]. In order to stop mixed-type comparisons from falling back to the default comparison by object address, when 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'0 object is compared to an object of a different type,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False081 is raised unless the comparison is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False075 or
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False076. The latter cases return
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'847 or
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'848, respectively
Changed in version 3. 3. Equality comparisons between aware and naive
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 instances don’t raise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False081.
In Boolean contexts, 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'0 object is always considered to be true
Changed in version 3. 5. Before Python 3. 5, 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'0 object was considered to be false if it represented midnight in UTC. This behavior was considered obscure and error-prone and has been removed in Python 3. 5. Xem bpo-13936 để biết chi tiết đầy đủ.
Other constructor
classmethod time. fromisoformat[time_string] ¶Trả lại một
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 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
Độ lệch múi giờ có thể có giây phân số
Enter the number of seconds:5000000 20:53:20
2380 hàng đầu, thường được yêu cầu trong trường hợp có thể có sự mơ hồ giữa ngày và giờ, không bắt buộcFractional seconds may have any number of digits [anything beyond 6 will be truncated]
Giờ và phút phân số không được hỗ trợ
ví dụ
Enter the number of seconds:5000000 20:53:20235
New in version 3. 7
Changed in version 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
Enter the number of seconds:5000000 20:53:20943.
Phương thức sơ thẩm
time. replace[hour=self. hour , minute=self. minute , second=self. giây , micro giây=chính mình. micro giây , tzinfo=self. tzinfo , * , fold=0] ¶Return 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'0 with the same value, except for those attributes given new values by whichever keyword arguments are specified. Note that
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False52 can be specified to create a naive
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 from an aware
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0, without conversion of the time data
New in version 3. 6. Added the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 argument. time. isoformat[timespec='auto'] ¶
Return a string representing the time in ISO 8601 format, one of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
80, nếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 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, ifdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 is 0Enter the number of seconds:5000000 20:53:20
953, if>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 does not return>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003Enter the number of seconds:5000000 20:53:20
956, ifdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 is 0 and>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 does not return>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003
Đố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ếudatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
77 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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74 ở đị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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74 vàdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
75 ở đị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ồmdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
74,datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
75 vàdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
76 ở đị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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 will be raised on an invalid timespec argument
Thí dụ
Enter the number of seconds:5000000 20:53:20236
Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian.
time. __str__[] ¶For a time t,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False055 is equivalent to
Enter the number of seconds:5000000 20:53:20983time. strftime[format] ¶
Return a string representing the time, controlled by an explicit format string. For a complete list of formatting directives, see strftime[] and strptime[] Behavior .
time. __format__[định dạng] ¶Same as
Enter the number of seconds:5000000 20:53:20984. This makes it possible to specify a format string for 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'0 object in formatted string literals and when using
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'874. For a complete list of formatting directives, see strftime[] and strptime[] Behavior . time. utcoffset[] ¶
If
>>> 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 is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, returns
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, else returns
Enter the number of seconds:5000000 20:53:20990, and raises an exception if the latter doesn’t return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 or a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object with magnitude less than one day
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
time. dst[] ¶If
>>> 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 is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, returns
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, else returns
Enter the number of seconds:5000000 20:53:20996, and raises an exception if the latter doesn’t return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, or a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object with magnitude less than one day
Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.
time. tzname[] ¶If
>>> 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 is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, returns
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, else returns
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]702, or raises an exception if the latter doesn’t return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 or a string object
Examples of Usage. >>> delta2 > delta1
True
>>> delta2 > 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
0¶
Examples of working with 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'0 object
Enter the number of seconds:5000000 20:53:20237
>>> 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 Objects¶
class datetime. tzinfo ¶This is an abstract base class, meaning that this class should not be instantiated directly. Define a subclass of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 to capture information about a particular time zone
An instance of [a concrete subclass of]
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 can be passed to the constructors for
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 and
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 objects. The latter objects view their attributes as being in local time, and the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 object supports methods revealing offset of local time from UTC, the name of the time zone, and DST offset, all relative to a date or time object passed to them
You need to derive a concrete subclass, and [at least] supply implementations of the standard
>>> 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 methods needed by the
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 methods you use. The
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 module provides
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8, a simple concrete subclass of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 which can represent timezones with fixed offset from UTC such as UTC itself or North American EST and EDT
Special requirement for pickling. 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'4 subclass must have an
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]718 method that can be called with no arguments, otherwise it can be pickled but possibly not unpickled again. This is a technical requirement that may be relaxed in the future
A concrete subclass of
>>> 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 may need to implement the following methods. Exactly which methods are needed depends on the uses made of aware
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 objects. If in doubt, simply implement all of themtzinfo. utcoffset[dt] ¶
Return offset of local time from UTC, as a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object that is positive east of UTC. If local time is west of UTC, this should be negative
This represents the total offset from UTC; for example, if a
>>> 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 object represents both time zone and DST adjustments,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 should return their sum. If the UTC offset isn’t known, return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003. Else the value returned must be a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object strictly between
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]726 and
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]727 [the magnitude of the offset must be less than one day]. Hầu hết các triển khai của
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 có thể sẽ giống như một trong hai
Enter the number of seconds:5000000 20:53:20238
If
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 does not return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 should not return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 either
The default implementation of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 raises
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]734
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
tzinfo. dst[dt] ¶Return the daylight saving time [DST] adjustment, as a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object or
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 if DST information isn’t known
Trả lại
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False083 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 [xem
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 để 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 do
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False57 trả về, do đó, không cần tham khảo
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 trừ khi bạn muốn nhận thông tin DST riêng. Ví dụ:
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'25 gọi phương thức
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 của thuộc tí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'4 để 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à
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False70 gọi
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 để giải thích cho các thay đổi DST khi vượt qua các múi giờ
Ví dụ tz của lớp con
>>> 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 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
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]749
must return the same result for every
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 dt with
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]751 For sane
>>> 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 subclasses, this expression yields the time zone’s “standard offset”, which should not depend on the date or the time, but only on geographic location. The implementation of
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]753 relies on this, but cannot detect violations; it’s the programmer’s responsibility to ensure it. If 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'4 subclass cannot guarantee this, it may be able to override the default implementation of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False70 to work correctly with
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False72 regardless
Most implementations of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 will probably look like one of these two
Enter the number of seconds:5000000 20:53:20239
or
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False0
The default implementation of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False58 raises
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]734
Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút.
tzinfo. tzname[dt] ¶Return the time zone name corresponding to the
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object dt, as a string. Nothing about string names is defined by the
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 module, and there’s no requirement that it mean anything in particular. Ví dụ: “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” are all valid replies. Return
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 if a string name isn’t known. Note that this is a method rather than a fixed string primarily because some
>>> 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 subclasses will wish to return different names depending on the specific value of dt passed, especially if the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 class is accounting for daylight time
The default implementation of
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]765 raises
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]734
These methods are called by a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 or
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 object, in response to their methods of the same names. A
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object passes itself as the argument, and 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'0 object passes
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 as the argument. 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'4 subclass’s methods should therefore be prepared to accept a dt argument of
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003, or of class
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7
When
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 is passed, it’s up to the class designer to decide the best response. For example, returning
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003 is appropriate if the class wishes to say that time objects don’t participate in the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 protocols. It may be more useful for
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]778 to return the standard UTC offset, as there is no other convention for discovering the standard offset
When a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object is passed in response to a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 method,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]781 is the same object as self.
>>> 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 methods can rely on this, unless user code calls
>>> 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 methods directly. The intent is that the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 methods interpret dt as being in local time, and not need worry about objects in other timezones
There is one more
>>> 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 method that a subclass may wish to overridetzinfo. fromutc[dt] ¶
This is called from the default
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]753 implementation. When called from that,
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]781 is self, and dt’s date and time data are to be viewed as expressing a UTC time. The purpose of
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]788 is to adjust the date and time data, returning an equivalent datetime in self’s local time
Most
>>> 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 subclasses should be able to inherit the default
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]788 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
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]788 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
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]788 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
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]788 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
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]795 file there are some examples of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 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
>>> 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 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. Một bức tường thời gian của mẫu 2. MM doesn’t really make sense on that day, so
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]798 won’t deliver a result with
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]799 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. there’s an hour that can’t be spelled unambiguously in local wall time. the last hour of daylight time. In Eastern, that’s times of the form 5. MM UTC on the day daylight time ends. The local wall clock leaps from 1. 59 [daylight time] back to 1. 00 [standard time] again. Local times of the form 1. MM are ambiguous.
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 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
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 instances that differ only by the value of the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 attribute are considered equal in comparisons
Applications that can’t bear wall-time ambiguities should explicitly check the value of the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'896 attribute or avoid using hybrid
>>> 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 subclasses; there are no ambiguities when using
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8, or any other fixed-offset
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 subclass [such as a class representing only EST [fixed offset -5 hours], or only EDT [fixed offset -4 hours]]
Xem thêm
IANA timezone database>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'1The
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 module has a basic>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8 class [for handling arbitrary fixed offsets from UTC] and itsEnter the number of seconds:5000000 20:53:207711 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
>>> delta2 > delta1
True
>>> delta2 > 5
Traceback [most recent call last]:
File "", line 1, in
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
8 Objects¶
The
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8 class is a subclass of
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4, 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
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 object representing the difference between the local time and UTC. It must be strictly between
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]726 and
datetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]727, otherwise
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093 is raised
The name argument is optional. If specified it must be a string that will be used as the value returned by the
Enter the number of seconds:5000000 20:53:207720 method
Mới trong phiên bản 3. 2
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
timezone. utcoffset[dt] ¶Return the fixed value specified when the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8 instance is constructed
The dt argument is ignored. The return value is a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False011 instance equal to the difference between the local time and UTC
Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
múi giờ. tzname[dt] ¶Return the fixed value specified when the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'8 instance is constructed
If name is not provided in the constructor, the name returned by
Enter the number of seconds:5000000 20:53:207724 is generated from the value of the
Enter the number of seconds:5000000 20:53:207725 as follows. If offset is
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False083, the name is “UTC”, otherwise it is a string in the format
Enter the number of seconds:5000000 20:53:207727, where ± is the sign of
Enter the number of seconds:5000000 20:53:207725, HH and MM are two digits of
Enter the number of seconds:5000000 20:53:207729 and
Enter the number of seconds:5000000 20:53:207730 respectively
Changed in version 3. 6. Tên được tạo từ
Enter the number of seconds:5000000 20:53:207731 hiện là
Enter the number of seconds:5000000 20:53:207732 đơn giản, không phải
Enter the number of seconds:5000000 20:53:207733. timezone. dst[dt] ¶
Always returns
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False003timezone. fromutc[dt] ¶
Return
Enter the number of seconds:5000000 20:53:207735. The dt argument must be an aware
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 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
Enter the number of seconds:5000000 20:53:207738
Thuộc tính lớp
timezone. utc ¶The UTC timezone,
Enter the number of seconds:5000000 20:53:207739
Enter the number of seconds:5000000
20:53:20
7740 and Enter the number of seconds:5000000
20:53:20
7741 Behavior¶
Enter the number of seconds:5000000 20:53:2092,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7, and
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 objects all support a
Enter the number of seconds:5000000 20:53:207745 method, to create a string representing the time under the control of an explicit format string
Conversely, the
Enter the number of seconds:5000000 20:53:207746 class method creates a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object from a string representing a date and time and a corresponding format string
The table below provides a high-level comparison of
Enter the number of seconds:5000000 20:53:207740 versus
Enter the number of seconds:5000000 20:53:207741
Enter the number of seconds:5000000 20:53:207750
Enter the number of seconds:5000000 20:53:207751
Usage
Convert object to a string according to a given format
Parse a string into a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7 object given a corresponding format
Type of method
Instance method
Class method
Method of
Enter the number of seconds:5000000 20:53:2092;
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7;
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False7
Signature
Enter the number of seconds:5000000 20:53:207745
Enter the number of seconds:5000000 20:53:207758
Enter the number of seconds:5000000
20:53:20
7740 and Enter the number of seconds:5000000
20:53:20
7741 Format Codes¶
The following is a list of all the format codes that the 1989 C standard requires, and these work on all platforms with a standard C implementation
Directive
Meaning
Example
Notes
Enter the number of seconds:5000000 20:53:207761
Weekday as locale’s abbreviated name
Sun, Mon, …, Sat [en_US];
So, Mo, …, Sa [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207762
Weekday as locale’s full name
Sunday, Monday, …, Saturday [en_US];
Sonntag, Montag, …, Samstag [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207763
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday
0, 1, …, 6
Enter the number of seconds:5000000 20:53:207764
Day of the month as a zero-padded decimal number
01, 02, …, 31
[9]
Enter the number of seconds:5000000 20:53:207765
Month as locale’s abbreviated name
Tháng 1, Tháng 2, …, Tháng 12 [en_US];
Jan, Feb, …, Dez [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207766
Month as locale’s full name
January, February, …, December [en_US];
Januar, Februar, …, Dezember [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207767
Month as a zero-padded decimal number
01, 02, …, 12
[9]
Enter the number of seconds:5000000 20:53:207768
Year without century as a zero-padded decimal number
00, 01, …, 99
[9]
Enter the number of seconds:5000000 20:53:207769
Year with century as a decimal number
0001, 0002, …, 2013, 2014, …, 9998, 9999
[2]
Enter the number of seconds:5000000 20:53:207770
Hour [24-hour clock] as a zero-padded decimal number
00, 01, …, 23
[9]
Enter the number of seconds:5000000 20:53:207771
Hour [12-hour clock] as a zero-padded decimal number
01, 02, …, 12
[9]
Enter the number of seconds:5000000 20:53:207772
Locale’s equivalent of either AM or PM
AM, PM [en_US];
am, pm [de_DE]
[1], [3]
Enter the number of seconds:5000000 20:53:207773
Minute as a zero-padded decimal number
00, 01, …, 59
[9]
Enter the number of seconds:5000000 20:53:207774
Second as a zero-padded decimal number
00, 01, …, 59
[4], [9]
Enter the number of seconds:5000000 20:53:207775
Microsecond as a decimal number, zero-padded to 6 digits
000000, 000001, …, 999999
[5]
Enter the number of seconds:5000000 20:53:207776
UTC offset in the form
Enter the number of seconds:5000000 20:53:207777 [empty string if the object is naive]
[empty], +0000, -0400, +1030, +063415, -030712. 345216
[6]
Enter the number of seconds:5000000 20:53:207778
Time zone name [empty string if the object is naive]
[empty], UTC, GMT
[6]
Enter the number of seconds:5000000 20:53:207779
Day of the year as a zero-padded decimal number
001, 002, …, 366
[9]
Enter the number of seconds:5000000 20:53:207780
Week number of the year [Sunday as the first day of the week] as a zero-padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0
00, 01, …, 53
[7], [9]
Enter the number of seconds:5000000 20:53:207781
Week number of the year [Monday as the first day of the week] as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0
00, 01, …, 53
[7], [9]
Enter the number of seconds:5000000 20:53:207782
Locale’s appropriate date and time representation
Tue Aug 16 21. 30. 00 1988 [en_US];
Di 16 Aug 21. 30. 00 1988 [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207783
Locale’s appropriate date representation
08/16/88 [None];
16/08/1988 [en_Mỹ];
16. 08. 1988 [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207784
Locale’s appropriate time representation
21. 30. 00 [en_US];
21. 30. 00 [de_DE]
[1]
Enter the number of seconds:5000000 20:53:207785
A literal
Enter the number of seconds:5000000 20:53:207786 character
%
Several additional directives not required by the C89 standard are included for convenience. These parameters all correspond to ISO 8601 date values
Directive
Meaning
Example
Notes
Enter the number of seconds:5000000 20:53:207787
ISO 8601 year with century representing the year that contains the greater part of the ISO week [
Enter the number of seconds:5000000 20:53:207788]
0001, 0002, …, 2013, 2014, …, 9998, 9999
[8]
Enter the number of seconds:5000000 20:53:207789
ISO 8601 weekday as a decimal number where 1 is Monday
1, 2, …, 7
Enter the number of seconds:5000000 20:53:207788
ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4
01, 02, …, 53
[8], [9]
These may not be available on all platforms when used with the
Enter the number of seconds:5000000 20:53:207740 method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling
Enter the number of seconds:5000000 20:53:207741 with incomplete or ambiguous ISO 8601 directives will raise a
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False093
The full set of format codes supported varies across platforms, because Python calls the platform C library’s
Enter the number of seconds:5000000 20:53:207740 function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime[3] documentation. There are also differences between platforms in handling of unsupported format specifiers
New in version 3. 6.
Enter the number of seconds:5000000 20:53:207787,
Enter the number of seconds:5000000 20:53:207789 and
Enter the number of seconds:5000000 20:53:207788 were added.
Technical Detail¶
Broadly speaking,
Enter the number of seconds:5000000 20:53:207798 acts like the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 module’s
date.strftime[format]500 although not all objects support 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'842 method
For the
Enter the number of seconds:5000000 20:53:207746 class method, the default value is
date.strftime[format]503. any components not specified in the format string will be pulled from the default value. 4
Using
date.strftime[format]504 is equivalent to
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'81
except when the format includes sub-second components or timezone offset information, which are supported in
date.strftime[format]505 but are discarded by
date.strftime[format]506
For
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'0 objects, the format codes for year, month, and day should not be used, 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'0 objects have no such values. If they’re used anyway,
date.strftime[format]509 is substituted for the year, and
Enter the number of seconds:5000000 20:53:2095 for the month and day
For
Enter the number of seconds:5000000 20:53:2092 objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as
Enter the number of seconds:5000000 20:53:2092 objects have no such values. If they’re used anyway,
>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False014 is substituted for them
For the same reason, handling of format strings containing Unicode code points that can’t be represented in the charset of the current locale is also platform-dependent. On some platforms such code points are preserved intact in the output, while on others
Enter the number of seconds:5000000 20:53:207750 may raise
date.strftime[format]515 or return an empty string instead
ghi chú
Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary [for example, “month/day/year” versus “day/month/year”], and the output may contain Unicode characters encoded using the locale’s default encoding [for example, if the current locale is
date.strftime[format]
516, the default encoding could be any one ofdate.strftime[format]
517,date.strftime[format]
518, ordate.strftime[format]
519; usedate.strftime[format]
520 to determine the current locale’s encoding]The
Enter the number of seconds:5000000 20:53:20
7741 method can parse years in the full [1, 9999] range, but years < 1000 must be zero-filled to 4-digit widthChanged in version 3. 2. In previous versions,
Enter the number of seconds:5000000 20:53:20
7740 method was restricted to years >= 1900.Changed in version 3. 3. In version 3. 2,
Enter the number of seconds:5000000 20:53:20
7740 method was restricted to years >= 1000.When used with the
Enter the number of seconds:5000000 20:53:20
7741 method, theEnter the number of seconds:5000000 20:53:20
7772 directive only affects the output hour field if theEnter the number of seconds:5000000 20:53:20
7771 directive is used to parse the hourUnlike the
>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
0 module, the>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 module does not support leap secondsWhen used with the
Enter the number of seconds:5000000 20:53:20
7741 method, theEnter the number of seconds:5000000 20:53:20
7775 directive accepts from one to six digits and zero pads on the right.Enter the number of seconds:5000000 20:53:20
7775 is an extension to the set of format characters in the C standard [but implemented separately in datetime objects, and therefore always available]For a naive object, the
Enter the number of seconds:5000000 20:53:20
7776 andEnter the number of seconds:5000000 20:53:20
7778 format codes are replaced by empty stringsFor an aware object
Enter the number of seconds:5000000 20:53:20
7776>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 is transformed into a string of the formEnter the number of seconds:5000000 20:53:20
7777, where>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
67 is a 2-digit string giving the number of UTC offset hours,date.strftime[format]
538 is a 2-digit string giving the number of UTC offset minutes,date.strftime[format]
539 is a 2-digit string giving the number of UTC offset seconds anddate.strftime[format]
540 is a 6-digit string giving the number of UTC offset microseconds. Thedate.strftime[format]
540 part is omitted when the offset is a whole number of seconds and both thedate.strftime[format]
540 and thedate.strftime[format]
539 part is omitted when the offset is a whole number of minutes. For example, if>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
57 returnsdate.strftime[format]
545,Enter the number of seconds:5000000 20:53:20
7776 is replaced with the stringdate.strftime[format]
547Đã thay đổi trong phiên bản 3. 7. Phần bù UTC không bị giới hạn trong một số nguyên của phút.
Changed in version 3. 7. When the
Enter the number of seconds:5000000 20:53:20
7776 directive is provided to theEnter the number of seconds:5000000 20:53:20
7741 method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example,date.strftime[format]
550 will be parsed as an offset of one hour. In addition, providingdate.strftime[format]
551 is identical todate.strftime[format]
552.Enter the number of seconds:5000000 20:53:20
7778In
Enter the number of seconds:5000000 20:53:20
7740,Enter the number of seconds:5000000 20:53:20
7778 is replaced by an empty string ifdatetime.timedelta[days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0]
765 returns>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
003; otherwiseEnter the number of seconds:5000000 20:53:20
7778 is replaced by the returned value, which must be a stringEnter the number of seconds:5000000 20:53:20
7741 only accepts certain values forEnter the number of seconds:5000000 20:53:20
7778any value in
date.strftime[format]
561 for your machine’s localethe hard-coded values
date.strftime[format]
562 anddate.strftime[format]
563
So someone living in Japan may have
date.strftime[format]
564,date.strftime[format]
562, anddate.strftime[format]
563 as valid values, but probably notdate.strftime[format]
567. It will raise>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
093 for invalid valuesChanged in version 3. 2. When the
Enter the number of seconds:5000000 20:53:20
7776 directive is provided to theEnter the number of seconds:5000000 20:53:20
7741 method, an aware>>> from datetime import timedelta >>> delta1 = timedelta[seconds=57] >>> delta2 = timedelta[hours=25, seconds=2] >>> delta2 != delta1 True >>> delta2 == 5 False
7 object will be produced. The>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
4 of the result will be set to a>>> delta2 > delta1 True >>> delta2 > 5 Traceback [most recent call last]: File "", line 1, in TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
8 instance.When used with the
Enter the number of seconds:5000000 20:53:20
7741 method,Enter the number of seconds:5000000 20:53:20
7780 andEnter the number of seconds:5000000 20:53:20
7781 are only used in calculations when the day of the week and the calendar year [Enter the number of seconds:5000000 20:53:20
7769] are specifiedSimilar to
Enter the number of seconds:5000000 20:53:20
7780 andEnter the number of seconds:5000000 20:53:20
7781,Enter the number of seconds:5000000 20:53:20
7788 is only used in calculations when the day of the week and the ISO year [Enter the number of seconds:5000000 20:53:20
7787] are specified in aEnter the number of seconds:5000000 20:53:20
7741 format string. Also note thatEnter the number of seconds:5000000 20:53:20
7787 andEnter the number of seconds:5000000 20:53:20
7769 are not interchangeableWhen used with the
Enter the number of seconds:5000000 20:53:20
7741 method, the leading zero is optional for formatsEnter the number of seconds:5000000 20:53:20
7764,Enter the number of seconds:5000000 20:53:20
7767,Enter the number of seconds:5000000 20:53:20
7770,Enter the number of seconds:5000000 20:53:20
7771,Enter the number of seconds:5000000 20:53:20
7773,Enter the number of seconds:5000000 20:53:20
7774,date.strftime[format]
592,Enter the number of seconds:5000000 20:53:20
7780,Enter the number of seconds:5000000 20:53:20
7781, andEnter the number of seconds:5000000 20:53:20
7788. FormatEnter the number of seconds:5000000 20:53:20
7768 does require a leading zero
Footnotes
1If, that is, we ignore the effects of Relativity
2This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations, where it’s the base calendar for all computations. 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