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 Show
Xem thêm mô-đunCác chức năng liên quan đến lịch chung mô-đunTime access and conversions mô-đunConcrete time zones representing the IANA time zone database Package dateutilThird-party library with expanded time zone and parsing support Aware and Naive ObjectsDate and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information With sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation. A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality For applications requiring aware objects, and objects have an optional time zone information attribute, >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4, that can be set to an instance of a subclass of the abstract class. Các đối tượng này nắm bắt thông tin về phần bù từ thời gian UTC, tên múi giờ và liệu thời gian tiết kiệm ánh sáng ban ngày có hiệu lực hay không Chỉ có một lớp cụ thể, lớp, được cung cấp bởi mô-đun. Lớp này có thể đại diện cho các múi giờ đơn giản với độ lệch cố định từ UTC, chẳng hạn như chính UTC hoặc các múi giờ EST và EDT của Bắc Mỹ. Hỗ trợ các múi giờ ở mức độ chi tiết sâu hơn tùy thuộc vào ứng dụng. Các quy tắc điều chỉnh thời gian trên toàn thế giới mang tính chính trị hơn là hợp lý, thay đổi thường xuyên và không có tiêu chuẩn nào phù hợp cho mọi ứng dụng ngoài UTC hằng sốMô-đun xuất các hằng số sau ngày giờ. MIN NĂMSố năm nhỏ nhất được phép trong một hoặc đối tượng. là >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05ngày giờ. TỐI ĐA NĂM Số năm lớn nhất được phép trong một hoặc đối tượng. là >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.09ngày giờ. UTC Bí danh cho múi giờ UTC đơn lẻ Mới trong phiên bản 3. 11 Các loại có sẵnlớ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. , , và lớp ngày giờ. thời gianThời gian lý tưởng hóa, không phụ thuộc vào bất kỳ ngày cụ thể nào, giả sử rằng mỗi ngày có chính xác 24*60*60 giây. (Không có khái niệm “giây nhuận” ở đây. ) Thuộc tính. , , , , và lớp ngày giờ. ngày giờMột sự kết hợp của một ngày và một thời gian. Thuộc tính. , , , , , , , Và lớp ngày giờ. đồng hồ thời gianKhoảng thời gian thể hiện sự khác biệt giữa hai , hoặc phiên bản đối với độ phân giải micro giây lớp ngày giờ. tzinfoMột lớp cơ sở trừu tượng cho các đối tượng thông tin múi giờ. Chúng được sử dụng bởi các lớp và để cung cấp khái niệm điều chỉnh thời gian có thể tùy chỉnh (ví dụ: để tính múi giờ và/hoặc thời gian tiết kiệm ánh sáng ban ngày) lớp ngày giờ. múi giờMột lớp triển khai lớp cơ sở trừu tượng dưới dạng phần bù cố định từ UTC Mới trong phiên bản 3. 2 Các đối tượng của các loại này là bất biến quan hệ phân lớp object timedelta tzinfo timezone time date datetime Tài sản chung, , và các loại chia sẻ các tính năng phổ biến này
Xác định xem một đối tượng là Aware hay NaiveCác đối tượng thuộc loại luôn ngây thơ An object of type or may be aware or naive Một đối tượng d nhận biết nếu cả hai điều sau đây đều đúng
Mặt khác, d là ngây thơ Một đối tượng t nhận biết nếu cả hai điều sau đây giữ
Nếu không, t là ngây thơ Sự khác biệt giữa nhận thức và ngây thơ không áp dụng cho các đối tượng Các đối tượngMột đối tượng đại diện cho một khoảng thời gian, sự khác biệt giữa hai ngày hoặc thời gian lớp ngày giờ. timedelta(ngày=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)Tất cả các đối số là tùy chọn và mặc định là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)14. Các đối số có thể là số nguyên hoặc số float và có thể dương hoặc âm Only days, seconds and microseconds are stored internally. Các đối số được chuyển đổi thành các đơn vị đó
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
Ví dụ sau đây minh họa cách mọi đối số ngoài ngày, giây và micro giây được “hợp nhất” và chuẩn hóa thành ba thuộc tính kết quả đó >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10) If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond using round-half-to-even tiebreaker. If no argument is a float, the conversion and normalization processes are exact (no information is lost) If the normalized value of days lies outside the indicated range, is raised Note that normalization of negative values may be surprising at first. For example >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) Class attributes timedelta. minThe most negative object, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)20timedelta. max The most positive object, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)22timedelta. resolution The smallest possible difference between non-equal objects, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)24 Note that, because of normalization, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)25 > >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)26. >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)27 is not representable as a object Instance attributes (read-only) Attribute Value >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)29 Between -999999999 and 999999999 inclusive >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)30 Between 0 and 86399 inclusive >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)31 Between 0 and 999999 inclusive Supported operations Operation Result >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)32 Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. (1) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)33 Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. (1)(6) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)34 Delta multiplied by an integer. Afterwards t1 // i == t2 is true, provided >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)35 In general, t1 * i == t1 * (i-1) + t1 is true. (1) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)36 Delta multiplied by a float. The result is rounded to the nearest multiple of timedelta. resolution using round-half-to-even >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)37 Chia (3) tổng thời lượng t2 cho đơn vị khoảng thời gian t3. Trả về một đối tượng >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)39 Delta chia cho float hoặc int. Kết quả được làm tròn đến bội số gần nhất của timedelta. độ phân giải sử dụng nửa vòng đến chẵn >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)40 hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)41 Sàn được tính và phần còn lại (nếu có) bị vứt đi. Trong trường hợp thứ hai, một số nguyên được trả về. (3) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)42 Phần còn lại được tính như một đối tượng. (3) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)44 Tính thương và số dư. >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)45 (3) và >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)46. q là một số nguyên và r là một đối tượng >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)48 Trả về một đối tượng có cùng giá trị. (2) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)50 tương đương với (-t1. ngày, -t1. giây, -t1. micro giây) và đến t1* -1. (1)(4) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)52 tương đương với +t khi >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)53, và tương đương với -t khi >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)54. (2) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)55 Trả về một chuỗi ở dạng >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)56, trong đó D là số âm cho số âm của >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)57. (5) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)58 Trả về một biểu diễn chuỗi của đối tượng dưới dạng lệnh gọi hàm tạo với các giá trị thuộc tính chuẩn ghi chú
Ngoài các hoạt động được liệt kê ở trên, các đối tượng hỗ trợ một số phép cộng và phép trừ với và đối tượng (xem bên dưới) Đã thay đổi trong phiên bản 3. 2. Phép chia tầng và phép chia thực sự của một đối tượng cho một đối tượng khác hiện đã được hỗ trợ, cũng như các phép toán còn lại và hàm. Phép chia thực sự và phép nhân của một đối tượng với một đối tượng hiện được hỗ trợ. So sánh các đối tượng được hỗ trợ, với một số lưu ý Các phép so sánh >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)75 hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)76 luôn trả về a , bất kể loại đối tượng được so sánh >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False Đối với tất cả các phép so sánh khác (chẳng hạn như >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)78 và >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)79), khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int' Trong ngữ cảnh Boolean, một đối tượng được coi là đúng khi và chỉ khi nó không bằng >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)83 Phương thức sơ thẩm đồng bằng thời gian. total_seconds()Trả về tổng số giây có trong khoảng thời gian. Tương đương với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)84. Đối với các đơn vị khoảng thời gian không phải là giây, hãy sử dụng biểu mẫu chia trực tiếp (e. g. >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)85) Lưu ý rằng trong khoảng thời gian rất lớn (hơn 270 năm trên hầu hết các nền tảng), phương pháp này sẽ mất độ chính xác micro giây Mới trong phiên bản 3. 2 Ví dụ về cách sử dụng.Một ví dụ bổ sung về chuẩn hóa >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.0 Ví dụ về số học >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3) Các đối tượngMột đối tượng đại diện cho một ngày (năm, tháng và ngày) trong lịch lý tưởng hóa, lịch Gregorian hiện tại được mở rộng vô thời hạn theo cả hai hướng Ngày 1 tháng 1 của năm 1 được gọi là ngày 1, ngày 2 tháng 1 của năm 1 được gọi là ngày 2, v.v. lớp ngày giờ. ngày(năm , tháng, day)All arguments are required. Các đối số phải là số nguyên, trong các phạm vi sau
Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên Các hàm tạo khác, tất cả các phương thức của lớp phương thức lớp ngày. hôm nay()Trả về ngày địa phương hiện tại Điều này tương đương với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)94phương thức lớp ngày. dấu thời gian từ(dấu thời gian) Trả về ngày địa phương tương ứng với dấu thời gian POSIX, chẳng hạn như được trả về bởi Điều này có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 của nền tảng và khi >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 thất bại. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038. Lưu ý rằng trên các hệ thống không phải POSIX bao gồm các giây nhuận trong khái niệm dấu thời gian, các giây nhuận bị bỏ qua bởi Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 của nền tảng. Tăng thay vì trên >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 thất bại. phương thức lớp ngày. từ thứ tự(thứ tự) Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1 được nâng lên trừ khi >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)08. Đối với bất kỳ ngày d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)09classmethod date. fromisoformat(date_string) Trả về một tương ứng với một chuỗi_ngày được cung cấp ở bất kỳ định dạng ISO 8601 hợp lệ nào, ngoại trừ các ngày thứ tự (e. g. >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)11) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) Mới trong phiên bản 3. 7 Changed in version 3. 11. Trước đây, phương thức này chỉ hỗ trợ định dạng >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)12. phương thức lớp ngày. từ dương lịch(năm , tuần, day) Trả về một tương ứng với ngày theo lịch ISO được chỉ định theo năm, tuần và ngày. Đây là nghịch đảo của hàm New in version 3. 8 Class attributes ngày. phútThe earliest representable date, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)15ngày. tối đa Ngày đại diện mới nhất, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)16date. độ phân giải The smallest possible difference between non-equal date objects, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)17 Instance attributes (read-only) ngày. nămGiữa và bao gồm ngày. monthTừ 1 đến 12 bao gồm ngày. ngàyGiữa 1 và số ngày trong tháng nhất định của năm nhất định Supported operations Operation Result >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)20 date2 will be >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)21 days after date1. (1) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)22 Tính date2 sao cho >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)23. (2) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)24 (3) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)25 date1 được coi là nhỏ hơn date2 khi date1 trước date2 về thời gian. (4) ghi chú
In Boolean contexts, all objects are considered to be true Phương thức sơ thẩm date. replace(year=self. năm , tháng=chính 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 Ví dụ >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)date. thời gian() Trả lại một chẳng hạn như được trả lại bởi The hours, minutes and seconds are 0, and the DST flag is -1 >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)52 tương đương với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)0 trong đó >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)53 là số ngày trong năm hiện tại bắt đầu bằng >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05 cho ngày 1 tháng 1ngày. toordinal() 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. For any object d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)09ngày. 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. Ví dụ, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)57, Thứ Tư. Xem thêmdate. isoweekday() Return the day of the week as an integer, where Monday is 1 and Sunday is 7. Ví dụ, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)59, Thứ Tư. Xem thêm ,ngày. isocalendar() Return a object with three components. >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)1, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)63 và >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)64 Lịch ISO là một biến thể được sử dụng rộng rãi của lịch Gregorian. Năm ISO bao gồm 52 hoặc 53 tuần đầy đủ và trong đó một tuần bắt đầu vào Thứ Hai và kết thúc vào Chủ Nhật. Tuần đầu tiên của một năm ISO là tuần dương lịch (Gregorian) đầu tiên của một năm có ngày thứ Năm. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004 >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)1 Changed in version 3. 9. Kết quả đã thay đổi từ một bộ thành một. ngày. isoformat()Return a string representing the date in ISO 8601 format, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)12 >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)2ngày. __str__() Đối với một ngày d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)66 tương đương với >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)67date. thời gian() Return a string representing the date >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)3 >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)68 is equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)4 trên các nền tảng có chức năng C >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)69 gốc (gọi, nhưng không gọi) tuân theo tiêu chuẩn Cdate. thời gian chạy(định dạng) Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. For a complete list of formatting directives, see date. __format__(format)Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see Examples of Usage.Example of counting days to an event >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)5 More examples of working with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)6 Các đối tượngA object is a single object containing all the information from a object and a object Like a object, assumes the current Gregorian calendar extended in both directions; like a object, assumes there are exactly 3600*24 seconds in every day Người xây dựng class datetime. ngày giờ(năm , tháng, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)The year, month and day arguments are required. tzinfo may be >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, or an instance of a subclass. The remaining arguments must be integers in the following ranges
Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên New in version 3. 6. Đã thêm đối số >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96. Các hàm tạo khác, tất cả các phương thức của lớp phương thức lớp ngày giờ. hôm nay()Trả về ngày giờ địa phương hiện tại, với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 Tương đương với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)7 See also , Phương thức này có chức năng tương đương với , nhưng không có tham số >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0002phương thức lớp ngày giờ. now(tz=None) Return the current local date and time Nếu đối số tùy chọn tz là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc không được chỉ định, thì điều này giống như , nhưng, nếu có thể, cung cấp độ chính xác cao hơn mức có thể nhận được từ việc xem xét dấu thời gian (ví dụ: điều này có thể thực hiện được trên các nền tảng cung cấp hàm C >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0006) Nếu tz không phải là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, thì nó phải là một thể hiện của một lớp con và ngày giờ hiện tại được chuyển đổi thành múi giờ của tz Chức năng này được ưu tiên hơn và phương thức lớp ngày giờ. utcnow()Trả về ngày và giờ UTC hiện tại, với >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 Điều này giống như, nhưng trả về ngày và giờ UTC hiện tại, dưới dạng một đối tượng ngây thơ. Có thể nhận được thời gian UTC hiện tại đã biết bằng cách gọi >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0015. Xem thêm Cảnh báo Bởi vì các đối tượng >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ được nhiều phương thức >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC. Như vậy, cách được đề xuất để tạo một đối tượng biểu thị thời gian hiện tại theo UTC là gọi số >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0015phương thức lớp ngày giờ. dấu thời gian từ(dấu thời gian , tz=None) Trả về ngày và giờ cục bộ tương ứng với dấu thời gian POSIX, chẳng hạn như được trả về bởi. Nếu đối số tùy chọn tz là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc không được chỉ định, thì dấu thời gian được chuyển đổi thành ngày giờ địa phương của nền tảng và đối tượng được trả về là ngây thơ If tz is not >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, it must be an instance of a subclass, and the timestamp is converted to tz’s time zone có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi các hàm C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 hoặc >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 của nền tảng và trên >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 hoặc >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 không thành công. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038. Lưu ý rằng trên các hệ thống không phải POSIX bao gồm giây nhuận trong khái niệm dấu thời gian, giây nhuận bị bỏ qua bởi , và sau đó có thể có hai dấu thời gian khác nhau một giây tạo ra các đối tượng giống hệt nhau. This method is preferred over Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi các hàm C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 hoặc ________49____28 của nền tảng. Raise instead of on >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 or >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 failure. Đã thay đổi trong phiên bản 3. 6. có thể trả về các trường hợp được đặt thành 1. phương thức lớp ngày giờ. utcfromtimestamp(dấu thời gian)Return the UTC corresponding to the POSIX timestamp, with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. (Đối tượng kết quả là ngây thơ. ) Điều này có thể tăng , nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 của nền tảng và khi >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 thất bại. Điều này thường bị giới hạn trong các năm từ 1970 đến 2038 Để có được một đối tượng nhận thức, hãy gọi >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)8 Trên nền tảng tuân thủ POSIX, nó tương đương với biểu thức sau >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)9 ngoại trừ công thức sau luôn hỗ trợ phạm vi năm đầy đủ. giữa và bao gồm Cảnh báo Bởi vì các đối tượng >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ được nhiều phương thức >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0058 Đã thay đổi trong phiên bản 3. 3. Tăng thay vì nếu dấu thời gian nằm ngoài phạm vi giá trị được hỗ trợ bởi hàm C >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 của nền tảng. Tăng thay vì trên >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 thất bại. phương thức lớp ngày giờ. từ thứ tự(thứ tự) Trả về tương ứng với thứ tự Gregorian proleptic, trong đó ngày 1 tháng 1 của năm 1 có thứ tự 1. được nâng lên trừ khi >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0067. The hour, minute, second and microsecond of the result are all 0, and is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03classmethod datetime. kết hợp(ngày , thời gian . tzinfo, tzinfo=self.tzinfo) Trả về một đối tượng mới có các thành phần ngày bằng với đối tượng đã cho và có thành phần thời gian bằng với đối tượng đã cho. If the tzinfo argument is provided, its value is used to set the attribute of the result, otherwise the attribute of the time argument is used For any object d, >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0076. Nếu ngày là một đối tượng, các thành phần và thuộc tính thời gian của nó sẽ bị bỏ qua Đã thay đổi trong phiên bản 3. 6. Đã thêm đối số tzinfo. phương thức lớp ngày giờ. fromisoformat(date_string)Return a corresponding to a date_string in any valid ISO 8601 format, with the following exceptions
ví dụ >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)0 Mới trong phiên bản 3. 7 Đã thay đổi trong phiên bản 3. 11. Trước đây, phương pháp này chỉ hỗ trợ các định dạng có thể được phát ra bởi hoặc. phương thức lớp ngày giờ. từ dương lịch(năm , tuần, day)Trả về một tương ứng với ngày theo lịch ISO được chỉ định theo năm, tuần và ngày. The non-date components of the datetime are populated with their normal default values. Đây là nghịch đảo của hàm New in version 3. 8 phương thức lớp ngày giờ. strptime(date_string , định dạng)Return a corresponding to date_string, parsed according to format Điều này tương đương với >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)1 is raised if the date_string and format can’t be parsed by or if it returns a value which isn’t a time tuple. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem Class attributes ngày giờ. minĐại diện sớm nhất , >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0089ngày giờ. tối đa Đại diện mới nhất , >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0091ngày giờ. độ phân giải The smallest possible difference between non-equal objects, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)24 Instance attributes (read-only) datetime. nămGiữa và bao gồm ngày giờ. thángTừ 1 đến 12 bao gồm ngày giờ. ngàyGiữa 1 và số ngày trong tháng nhất định của năm nhất định ngày giờ. giờTrong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0096ngày giờ. phút Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097ngày giờ. giây Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097ngày giờ. micro giây Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0099ngày giờ. tzinfo Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 nếu không có đối số nào được truyềnngày giờ. gấp Trong >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. (Một khoảng thời gian lặp lại xảy ra khi đồng hồ đượ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 Supported operations Operation Result >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False03 (1) >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False04 (2) >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False05 (3) >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False06 So sánh với. (4)
Phương thức sơ thẩm ngày giờ. ngày()Trả về đối tượng có cùng năm, tháng và ngày ngày giờ. thời gian()Trả về đối tượng có cùng giờ, phút, giây, micro giây và lần. là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. Xem thêm phương pháp Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về. ngày giờ. timetz()Trả về đối tượng có cùng thuộc tính giờ, phút, giây, micro giây, nếp gấp và tzinfo. Xem thêm phương pháp Đã thay đổi trong phiên bản 3. 6. Giá trị của màn hình đầu tiên được sao chép vào đối tượng được trả về. ngày giờ. thay thế(năm=chính mình. năm , tháng=chính 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. Đã thêm đối số >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96. ngày giờ. múi giờ(tz=Không có) Trả về một đối tượng có thuộc tính mới tz, điều chỉnh dữ liệu ngày và giờ để kết quả có cùng thời gian UTC với chính nó, nhưng theo giờ địa phương của tz Nếu được cung cấp, tz phải là một thể hiện của một lớp con và các phương thức and của nó không được trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. Nếu bản thân là ngây thơ, nó được coi là đại diện cho thời gian trong múi giờ hệ thống Nếu được gọi mà không có đối số (hoặc với >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False60), múi giờ cục bộ của hệ thống được giả định cho múi giờ đích. Thuộc tính >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False61 của phiên bản datetime đã chuyển đổi sẽ được đặt thành phiên bản có tên vùng và phần bù thu được từ HĐH Nếu >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False63 là tz, thì >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False64 là tự. không thực hiện điều chỉnh dữ liệu ngày hoặc giờ. Mặt khác, kết quả là giờ địa phương trong múi giờ tz, biểu thị cùng thời gian UTC với chính mình. sau >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False65, >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False66 sẽ có cùng dữ liệu ngày và giờ như >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False67 Nếu bạn chỉ muốn đính kèm một đối tượng múi giờ tz vào một datetime dt mà không đ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 Note that the default method can be overridden in a subclass to affect the result returned by . Ignoring error cases, acts like >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)2 Đã thay đổi trong phiên bản 3. 3. tz bây giờ có thể được bỏ qua. Đã thay đổi trong phiên bản 3. 6. Phương thức hiện có thể được gọi trên các phiên bản ngây thơ được cho là đại diện cho giờ địa phương của hệ thống. ngày giờ. utcoffset()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, nếu không thì trả về >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False78 và đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng có cường độ nhỏ hơn một ngày Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. ngày giờ. dst()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, nếu không thì trả về >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False84 và đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng có cường độ nhỏ hơn một ngày Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút. ngày giờ. tzname()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, ngược lại trả về >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False90, đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng chuỗi,ngày giờ. thời gian() Trả lại một chẳng hạn như được trả lại bởi >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)52 tương đương với >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)3 trong đó >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)53 là số ngày trong năm hiện tại bắt đầu bằng >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05 cho ngày 1 tháng 1. Cờ >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 của kết quả được thiết lập theo phương thức. là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 được đặt thành >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'04; ngày giờ. utctimetuple() If instance d is naive, this is the same as >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)52 except that >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 is forced to 0 regardless of what >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'13 returns. DST không bao giờ có hiệu lực trong thời gian UTC Nếu d biết, d được chuẩn hóa thành thời gian UTC, bằng cách trừ đi >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'14 và a cho thời gian chuẩn hóa được trả về. >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 buộc phải 0. Lưu ý rằng an có thể tăng nếu d. năm là >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.04 hoặc >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.08 và sự điều chỉnh của UTC tràn qua ranh giới năm Cảnh báo Bởi vì các đối tượng >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 ngây thơ được nhiều phương thức >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 coi là giờ địa phương, nên sử dụng thời gian nhận biết để biểu thị thời gian theo UTC; . If you have a naive >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 representing UTC, use >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'24 to make it aware, at which point you can use ngày giờ. bình thường() Trả về thứ tự Gregorian proleptic của ngày. Giống như >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> 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. Giá trị trả về tương tự như giá trị được trả về bởi Các phiên bản 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 "<stdin>", line 1, in <module> 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ì hỗ trợ phạm vi giá trị rộng hơn >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> 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 trong thời gian xa trong quá khứ hoặc xa trong tương lai Đối với các trường hợp nhận biết, giá trị trả về được tính là >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)4 Mới trong phiên bản 3. 3 Đã thay đổi trong phiên bản 3. 6. Phương thức sử dụng thuộc tính để phân biệt thời gian trong một khoảng thời gian lặp lại. Ghi chú 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 ngây thơ biểu thị 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, thì bạn có thể lấy dấu thời gian POSIX bằng cách cung cấp >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'39 >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)5 hoặc bằng cách tính toán dấu thời gian trực tiếp >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)6ngày giờ. ngày trong tuần() Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 0 và Chủ nhật là 6. Giống như >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'40. Xem thêmngày giờ. ngày trong tuần() Trả về ngày trong tuần dưới dạng số nguyên, trong đó Thứ Hai là 1 và Chủ Nhật là 7. Giống như >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'42. Xem thêm ,ngày giờ. isocalendar() Trả về a với ba thành phần. >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)1, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)63 và >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)64. Giống như >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'48ngày giờ. isoformat(sep=', timespec='auto') Trả về một chuỗi biểu thị ngày và giờ ở định dạng ISO 8601
Nếu không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, một chuỗi sẽ được thêm vào, tạo ra phần bù UTC
ví dụ >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)7 Đối số tùy chọn sep (mặc định >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'59) là dấu phân tách một ký tự, được đặt giữa phần ngày và giờ của kết quả. Ví dụ >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)8 Đối số tùy chọn timespec chỉ định số lượng thành phần bổ sung của thời gian cần đưa vào (mặc định là >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'60). Nó có thể là một trong những điều sau đây
Ghi chú Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn sẽ được nâng lên trên một đối số timespec không hợp lệ >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)9 Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian. ngày giờ. __str__()Đối với trường hợp d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)66 tương đương với >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'84ngày giờ. thời gian() Trả về một chuỗi đại diện cho ngày và giờ >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:000 Chuỗi đầu ra sẽ không bao gồm thông tin múi giờ, bất kể đầu vào là nhận biết hay ngây thơ >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)68 is equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)4 trên các nền tảng có chức năng C >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)69 gốc (gọi, nhưng không gọi) tuân theo tiêu chuẩn Cngày giờ. thời gian chạy(định dạng) Trả về một chuỗi đại diện cho ngày và giờ, được kiểm soát bởi một chuỗi định dạng rõ ràng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem ngày giờ. __format__(định dạng)Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see Examples of Usage.Ví dụ về làm việc với các đối tượng >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:002 Ví dụ bên dưới định nghĩa một lớp con thu thập thông tin múi giờ cho Kabul, Afghanistan, sử dụng +4 UTC cho đến năm 1945 và sau đó là +4. 30 UTC sau đó >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:003 Cách sử dụng của >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'95 từ phía trên >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:004 Các đối tượngMột đối tượng đại diện cho thời gian (cục bộ) trong ngày, không phụ thuộc vào bất kỳ ngày cụ thể nào và có thể điều chỉnh thông qua một đối tượng lớp ngày giờ. thời gian(giờ=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)Tất cả các đối số là tùy chọn. tzinfo có thể là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một thể hiện của lớp con. Các đối số còn lại phải là số nguyên trong các phạm vi sau
Nếu một đối số bên ngoài các phạm vi được đưa ra, được nâng lên. Tất cả mặc định là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)14 ngoại trừ tzinfo, mặc định là Class attributes thời gian. phútĐại diện sớm nhất, >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.010thời gian. tối đa Đại diện mới nhất , >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.012thời gian. độ phân giải Sự khác biệt nhỏ nhất có thể giữa các đối tượng không bằng nhau, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)24, mặc dù lưu ý rằng số học trên các đối tượng không được hỗ trợ Instance attributes (read-only) thời gian. giờTrong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0096thời gian. phút Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097thời gian. giây Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097thời gian. micro giây Trong >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0099thời gian. tzinfo Đối tượng được truyền dưới dạng đối số tzinfo cho hàm tạo hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 nếu không có đối số nào được truyềnthời gian. gấp Trong >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False02. Được sử dụng để phân biệt thời gian tường trong một khoảng thời gian lặp lại. (Một khoảng thời gian lặp lại xảy ra khi đồng hồ đượ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 các đối tượng hỗ trợ so sánh với , trong đó a được coi là nhỏ hơn b khi a đứng trước b về thời gian. Nếu một so sánh là ngây thơ và người kia nhận thức được, sẽ được nâng lên nếu cố gắng so sánh thứ tự. Để so sánh bình đẳng, các trường hợp ngây thơ không bao giờ bằng các trường hợp nhận thức Nếu cả hai đối tượng so sánh đều nhận biết và có cùng thuộc tính, thuộc tính chung sẽ bị bỏ qua và thời gian cơ sở được so sánh. Nếu cả hai bộ so sánh đều nhận biết và có các thuộc tính khác nhau, thì các bộ so sánh trước tiên được điều chỉnh bằng cách trừ đi độ lệch UTC của chúng (thu được từ >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False30). Để ngăn các so sánh kiểu hỗn hợp quay trở lại so sánh mặc định theo địa chỉ đối tượng, khi một đối tượng được so sánh với một đối tượng thuộc loại khác, sẽ được nâng lên trừ khi phép so sánh là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)75 hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)76. Các trường hợp sau trả về hoặc , tương ứng Đã thay đổi trong phiên bản 3. 3. So sánh bình đẳng giữa các trường hợp nhận thức và ngây thơ không nâng cao. Trong ngữ cảnh Boolean, một đối tượng luôn được coi là đúng Đã thay đổi trong phiên bản 3. 5. Trước Python 3. 5, một đối tượng được coi là sai nếu nó biểu thị nửa đêm theo giờ UTC. Hành vi này được coi là tối nghĩa và dễ bị lỗi và đã bị xóa trong Python 3. 5. Xem bpo-13936 để biết chi tiết đầy đủ. nhà xây dựng khác phương pháp phân loại thời gian. fromisoformat(time_string)Trả về một tương ứng với một time_string ở bất kỳ định dạng ISO 8601 hợp lệ nào, với các ngoại lệ sau
ví dụ >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:005 Mới trong phiên bản 3. 7 Đã thay đổi trong phiên bản 3. 11. Trước đây, phương pháp này chỉ hỗ trợ các định dạng có thể được phát ra bởi. Phương thức sơ thẩm thời gian. thay thế(giờ=chính mình. giờ , phút=chính mình. phút , giây=chính mình. giây , micro giây=chính mình. micro giây , tzinfo=self. tzinfo , * , gấp=0)Trả về a có cùng giá trị, ngoại trừ các thuộc tính được cung cấp giá trị mới theo bất kỳ đối số từ khóa nào được chỉ định. Lưu ý rằng có thể chỉ định >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False52 để tạo ngây thơ từ một nhận thức mà không cần chuyển đổi dữ liệu thời gian New in version 3. 6. Đã thêm đối số >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96. thời gian. isoformat(timespec=') Trả về một chuỗi biểu thị thời gian ở định dạng ISO 8601, một trong
Đố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 "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'60). Nó có thể là một trong những điều sau đây
Ghi chú Các thành phần thời gian bị loại trừ bị cắt bớt, không được làm tròn sẽ được nâng lên trên một đối số timespec không hợp lệ Ví dụ >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:006 Mới trong phiên bản 3. 6. Đã thêm đối số thông số thời gian. thời gian. __str__()Trong thời gian t, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)55 tương đương với >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.083thời gian. thời gian chạy(định dạng) Trả về một chuỗi đại diện cho thời gian, được kiểm soát bởi một chuỗi định dạng rõ ràng. Để biết danh sách đầy đủ các chỉ thị định dạng, hãy xem thời gian. __format__(định dạng)Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see thời gian. utcoffset()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, nếu không thì trả về >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.090 và đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng có cường độ nhỏ hơn một ngày Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. thời gian. dst()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, nếu không thì trả về >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.096 và đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng có cường độ nhỏ hơn một ngày Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút. thời gian. tzname()Nếu là >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, ngược lại trả về >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)02 hoặc đưa ra một ngoại lệ nếu cái sau không trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc một đối tượng chuỗi Examples of Usage.Ví dụ làm việc với đối tượng >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:007 Các đối tượnglớp ngày giờ. tzinfoĐây là một lớp cơ sở trừu tượng, có nghĩa là lớp này không được khởi tạo trực tiếp. Xác định một lớp con của để nắm bắt thông tin về một múi giờ cụ thể Một thể hiện của (một lớp con cụ thể của) có thể được truyền cho các hàm tạo cho và các đối tượng. Các đối tượng thứ hai xem các thuộc tính của chúng là theo giờ địa phương và đối tượng hỗ trợ các phương thức tiết lộ phần bù giờ địa phương từ UTC, tên của múi giờ và phần bù DST, tất cả đều liên quan đến đối tượng ngày hoặc giờ được truyền cho chúng Bạn cần lấy được một lớp con cụ thể và (ít nhất) cung cấp các triển khai của các phương thức tiêu chuẩn cần thiết cho các phương thức bạn sử dụng. Mô-đun cung cấp , một phân lớp cụ thể đơn giản trong đó có thể biểu thị các múi giờ với phần bù cố định từ UTC, chẳng hạn như chính UTC hoặc EST và EDT của Bắc Mỹ Yêu cầu đặc biệt đối với dưa chua. Một lớp con phải có một phương thức >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)18 có thể được gọi mà không có đối số, nếu không, nó có thể được chọn nhưng có thể không được giải nén lại. Đây là yêu cầu kỹ thuật có thể được nới lỏng trong tương lai Một lớp con cụ thể của có thể cần phải thực hiện các phương pháp sau. Chính xác những phương pháp nào là cần thiết phụ thuộc vào việc sử dụng các đối tượng nhận biết. Nếu nghi ngờ, chỉ cần thực hiện tất cả chúng tzinfo. utcoffset(dt)Trả về phần bù của giờ địa phương từ UTC, dưới dạng một đối tượng dương ở phía đông của UTC. Nếu giờ địa phương ở phía tây của UTC, điều này sẽ là âm Điều này thể hiện tổng phần bù từ UTC; . Nếu không biết phần bù UTC, hãy trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. Mặt khác, giá trị được trả về phải là một đối tượng hoàn toàn nằm trong khoảng từ >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)26 đến >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)27 (độ lớn của phần bù phải nhỏ hơn một ngày). Hầu hết các triển khai có thể sẽ giống như một trong hai >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:008 Nếu không trả lại >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, thì cũng không nên trả lại >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 Việc thực hiện tăng mặc định Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. tzinfo. dst(dt)Trả lại điều chỉnh thời gian tiết kiệm ánh sáng ban ngày (DST), dưới dạng đối tượng hoặc >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 nếu thông tin DST không được biết Trả lại >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)83 nếu DST không có hiệu lực. Nếu DST có hiệu lực, hãy trả lại phần bù dưới dạng đối tượng (xem để biết chi tiết). Lưu ý rằng phần bù DST, nếu có, đã được thêm vào phần bù UTC được trả về bởi , do đó, không cần tham khảo trừ khi bạn muốn nhận thông tin DST riêng. Ví dụ: gọi phương thức thuộc tính của nó để xác định cách đặt cờ >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 và gọi để giải thích cho các thay đổi DST khi vượt qua các múi giờ Một thể hiện tz của một lớp con mô hình hóa cả thời gian tiêu chuẩn và thời gian ban ngày phải nhất quán theo nghĩa này >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)49 phải trả về cùng một kết quả cho mọi dt với >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)51 Đối với các lớp con lành mạnh, biểu thức này mang lại "độ lệch chuẩn" của múi giờ, không phụ thuộc vào ngày hoặc giờ mà chỉ phụ thuộc vào vị trí địa lý. Việc thực hiện ỷ lại vào việc này nhưng không phát hiện được vi phạm; . Nếu một lớp con không thể đảm bảo điều này, thì nó có thể ghi đè cài đặt mặc định của để hoạt động chính xác với >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False72 bất kể Hầu hết các triển khai có thể sẽ giống như một trong hai >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:009 hoặc >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False0 Việc thực hiện tăng mặc định Đã thay đổi trong phiên bản 3. 7. DST offset không bị giới hạn trong một số phút. tzinfo. tzname(dt)Trả về tên múi giờ tương ứng với đối tượng dt, dưới dạng chuỗi. Không có gì về tên chuỗi được xác định bởi mô-đun và không có yêu cầu rằng nó có ý nghĩa cụ thể. Ví dụ: “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” đều là những câu trả lời hợp lệ. Trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 nếu không biết tên chuỗi. Lưu ý rằng đây chủ yếu là một phương thức chứ không phải là một chuỗi cố định bởi vì một số lớp con sẽ muốn trả về các tên khác nhau tùy thuộc vào giá trị cụ thể của dt được truyền, đặc biệt nếu lớp đó đang chiếm thời gian ban ngày Việc thực hiện tăng mặc định Các phương thức này được gọi bởi một hoặc đối tượng, để đáp lại các phương thức cùng tên của chúng. Một đối tượng chuyển chính nó làm đối số và một đối tượng chuyển >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 làm đối số. Do đó, các phương thức của lớp con nên được chuẩn bị để chấp nhận đối số dt của >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 hoặc của lớp Khi >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 được thông qua, người thiết kế lớp sẽ quyết định câu trả lời tốt nhất. Ví dụ: trả về >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 là phù hợp nếu lớp muốn nói rằng các đối tượng thời gian không tham gia vào các giao thức. Có thể hữu ích hơn khi >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)78 trả về phần bù UTC tiêu chuẩn, vì không có quy ước nào khác để khám phá phần bù tiêu chuẩn Khi một đối tượng được truyền để phản hồi lại một phương thức, thì >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)81 chính là đối tượng như self. các phương thức có thể dựa vào điều này, trừ khi mã người dùng gọi trực tiếp các phương thức. Mục đích là các phương thức diễn giải dt theo giờ địa phương và không cần lo lắng về các đối tượng trong các múi giờ khác Có một phương thức nữa mà một lớp con có thể muốn ghi đè tzinfo. từutc(dt)Điều này được gọi từ việc thực hiện mặc định. Khi được gọi từ đó, >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)81 là chính nó và dữ liệu ngày và giờ của dt sẽ được xem là thể hiện thời gian UTC. Mục đích của là điều chỉnh dữ liệu ngày và giờ, trả về một ngày giờ tương đương theo giờ địa phương của chính bạn Hầu hết các lớp con sẽ có thể kế thừa việc triển khai mặc định mà không gặp sự cố. Nó đủ mạnh để xử lý các múi giờ có độ lệch cố định và các múi giờ tính cả thời gian tiêu chuẩn và thời gian ban ngày, và cả thời gian sau ngay cả khi thời gian chuyển đổi DST khác nhau trong các năm khác nhau. Một ví dụ về múi giờ mà việc triển khai mặc định có thể không xử lý chính xác trong mọi trường hợp là trường hợp mà độ lệch tiêu chuẩn (từ UTC) phụ thuộc vào ngày và giờ cụ thể đã trôi qua, điều này có thể xảy ra vì lý do chính trị. Việc triển khai mặc định của >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False72 và có thể không tạo ra kết quả bạn muốn nếu kết quả là một trong những giờ diễn ra vào thời điểm giá trị bù tiêu chuẩn thay đổi Bỏ qua mã cho các trường hợp lỗi, việc triển khai mặc định hoạt động như >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False1 Trong tệp >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)95 sau đây có một số ví dụ về các lớp >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False2 Note that there are unavoidable subtleties twice per year in a subclass accounting for both standard and daylight time, at the DST transition points. For concreteness, consider US Eastern (UTC -0500), where EDT begins the minute after 1. 59 (EST) on the second Sunday in March, and ends the minute after 1. 59 (EDT) on the first Sunday in November >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False3 When DST starts (the “start” line), the local wall clock leaps from 1. 59 to 3. 00. A wall time of the form 2. MM doesn’t really make sense on that day, so >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)98 won’t deliver a result with >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)99 on the day DST begins. For example, at the Spring forward transition of 2016, we get >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False4 When DST ends (the “end” line), there’s a potentially worse problem. 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 attribute set to 0 and the later times have it set to 1. Ví dụ: tại Fall back transition năm 2016, chúng tôi nhận được >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False5 Note that the instances that differ only by the value of the attribute are considered equal in comparisons Applications that can’t bear wall-time ambiguities should explicitly check the value of the attribute or avoid using hybrid subclasses; there are no ambiguities when using , or any other fixed-offset subclass (such as a class representing only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)) Xem thêm IANA timezone database The Time Zone Database (often called tz, tzdata or zoneinfo) contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules Các đối tượngThe class is a subclass of , each instance of which represents a timezone defined by a fixed offset from UTC Objects of this class cannot be used to represent timezone information in the locations where different offsets are used in different days of the year or where historical changes have been made to civil time class datetime. timezone(offset , name=None)The offset argument must be specified as a object representing the difference between the local time and UTC. It must be strictly between >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)26 and >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)27, otherwise is raised The name argument is optional. If specified it must be a string that will be used as the value returned by the method Mới trong phiên bản 3. 2 Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. timezone. utcoffset(dt)Return the fixed value specified when the instance is constructed The dt argument is ignored. The return value is a instance equal to the difference between the local time and UTC Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. timezone. tzname(dt)Return the fixed value specified when the instance is constructed If name is not provided in the constructor, the name returned by >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)24 is generated from the value of the >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)25 as follows. If offset is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)83, the name is “UTC”, otherwise it is a string in the format >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)27, where ± is the sign of >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)25, HH and MM are two digits of >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)29 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)30 respectively Changed in version 3. 6. Name generated from >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)31 is now plain >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)32, not >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)33. timezone. dst(dt) Always returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03timezone. fromutc(dt) Return >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)35. The dt argument must be an aware instance, with >>> delta2 > delta1 True >>> delta2 > 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'4 set to >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)38 Class attributes timezone. utcThe UTC timezone, >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)39 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 40 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 41 Behavior, , and objects all support a >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)45 method, to create a string representing the time under the control of an explicit format string Conversely, the class method creates a object from a string representing a date and time and a corresponding format string The table below provides a high-level comparison of >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)40 versus >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)41 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)50 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)51 Usage Convert object to a string according to a given format Parse a string into a object given a corresponding format Type of method Instance method Class method Method of ; ; Signature >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)45 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)58 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 40 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4) 41 Format CodesThe 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 Ví dụ Notes >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)61 Weekday as locale’s abbreviated name Sun, Mon, …, Sat (en_US); So, Mo, …, Sa (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)62 Weekday as locale’s full name Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)63 Weekday as a decimal number, where 0 is Sunday and 6 is Saturday 0, 1, …, 6 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)64 Day of the month as a zero-padded decimal number 01, 02, …, 31 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)65 Month as locale’s abbreviated name Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)66 Month as locale’s full name January, February, …, December (en_US); Januar, Februar, …, Tháng mười hai (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)67 Tháng dưới dạng số thập phân không đệm 01, 02, …, 12 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)68 Năm không có thế kỷ dưới dạng số thập phân không đệm 00, 01, …, 99 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)69 Year with century as a decimal number 0001, 0002, …, 2013, 2014, …, 9998, 9999 (2) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)70 Hour (24-hour clock) as a zero-padded decimal number 00, 01, …, 23 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)71 Hour (12-hour clock) as a zero-padded decimal number 01, 02, …, 12 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)72 Locale’s equivalent of either AM or PM AM, PM (en_US); am, pm (de_DE) (1), (3) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)73 Minute as a zero-padded decimal number 00, 01, …, 59 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)74 Second as a zero-padded decimal number 00, 01, …, 59 (4), (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)75 Micro giây dưới dạng số thập phân, được đệm bằng 0 thành 6 chữ số 000000, 000001, …, 999999 (5) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)76 UTC offset in the form >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)77 (empty string if the object is naive) (empty), +0000, -0400, +1030, +063415, -030712. 345216 (6) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)78 Time zone name (empty string if the object is naive) (empty), UTC, GMT (6) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)79 Day of the year as a zero-padded decimal number 001, 002, …, 366 (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)80 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) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)81 Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0 00, 01, …, 53 (7), (9) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)82 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) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)83 Locale’s appropriate date representation 08/16/88 (None); 08/16/1988 (en_US); 16. 08. 1988 (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)84 Locale’s appropriate time representation 21. 30. 00 (en_US); 21. 30. 00 (de_DE) (1) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)85 A literal >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)86 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 Ví dụ Notes >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)87 ISO 8601 year with century representing the year that contains the greater part of the ISO week ( >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)88) 0001, 0002, …, 2013, 2014, …, 9998, 9999 (8) >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)89 ISO 8601 weekday as a decimal number where 1 is Monday 1, 2, …, 7 >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)88 ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4 01, 02, …, 53 (8), (9) Những thứ này có thể không khả dụng trên tất cả các nền tảng khi được sử dụng với phương pháp >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)40. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)41 with incomplete or ambiguous ISO 8601 directives will raise a The full set of format codes supported varies across platforms, because Python calls the platform C library’s >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)40 function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. There are also differences between platforms in handling of unsupported format specifiers New in version 3. 6. >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)87, >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)89 and >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)88 were added. Technical DetailBroadly speaking, >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)98 acts like the module’s >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)00 although not all objects support a >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)42 method For the class method, the default value is >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)03. any components not specified in the format string will be pulled from the default value. Using >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)04 is equivalent to >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)1 except when the format includes sub-second components or timezone offset information, which are supported in >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)05 but are discarded by >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)06 For objects, the format codes for year, month, and day should not be used, as objects have no such values. If they’re used anyway, >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)09 is substituted for the year, and >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05 for the month and day For objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as objects have no such values. If they’re used anyway, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)14 is substituted for them For the same reason, handling of format strings containing Unicode code points that can’t be represented in the charset of the current locale is also platform-dependent. On some platforms such code points are preserved intact in the output, while on others >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)50 may raise or return an empty string instead ghi chú
Footnotes If, that is, we ignore the effects of Relativity This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations, where it’s the base calendar for all computations. See the book for algorithms for converting between proleptic Gregorian ordinals and many other calendar systems How to convert string to ISO date in Python?This string is converted into an ISO format string by using the . strftime() method . Here as we know that ISO format is YYYY-MM-DD so we convert it into this format by using the following format code- “%Y-%m-%dT%H. %M. %S. %f%z”.
How to format date to ISO in Python?In Python ISO 8601 date is represented in YYYY-MM-DDTHH. MM. SS. mmmmmm format. For example, May 18, 2022, is represented as 2022-05-18T11. 40. 22. 519222.
How to format datetime to ISO?The ISO standard takes a general-to-specific approach for its date formats. The year comes first, followed by the month and then by the day of the month , with most elements represented as numerical values. For example, the ISO representation for July 15, 2022 is either 20220715 or 2022-07-15.
How to convert string timestamp to date in Python?Use the datetime. hàm strptime() (định dạng dấu thời gian ở định dạng chuỗi thành đối tượng ngày-giờ) để chuyển đổi dấu thời gian thành đối tượng ngày giờ bằng cách chuyển dấu thời gian đầu vào và . In đối tượng datetime kết quả. |