Sementara aritmatika tanggal dan waktu didukung, fokus penerapannya adalah pada ekstraksi atribut yang efisien untuk pemformatan dan manipulasi output Show
Lihat juga ModulFungsi terkait kalender umum ModulAkses waktu dan konversi ModulZona waktu konkret yang mewakili basis data zona waktu IANA Penggunaan tanggal paketPustaka pihak ketiga dengan zona waktu yang diperluas dan dukungan parsing Objek Sadar dan NaifObjek tanggal dan waktu dapat dikategorikan sebagai "sadar" atau "naif" bergantung pada apakah mereka menyertakan informasi zona waktu atau tidak Dengan pengetahuan yang memadai tentang penyesuaian waktu algoritmik dan politik yang berlaku, seperti zona waktu dan informasi waktu musim panas, objek sadar dapat menempatkan dirinya relatif terhadap objek sadar lainnya. Objek sadar mewakili momen tertentu dalam waktu yang tidak terbuka untuk interpretasi. Objek naif tidak berisi informasi yang cukup untuk menempatkan dirinya secara jelas relatif terhadap objek tanggal/waktu lainnya. Apakah objek naif mewakili Coordinated Universal Time (UTC), waktu lokal, atau waktu di beberapa zona waktu lain murni tergantung pada program, sama seperti program apakah angka tertentu mewakili meter, mil, atau massa. Objek naif mudah dipahami dan dikerjakan, dengan mengorbankan beberapa aspek realitas Untuk aplikasi yang membutuhkan objek sadar, dan objek memiliki atribut informasi zona waktu opsional, >>> 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, yang dapat disetel ke turunan subkelas dari kelas abstrak. Objek ini menangkap informasi tentang offset dari waktu UTC, nama zona waktu, dan apakah waktu musim panas berlaku Hanya satu kelas konkret, yaitu kelas, yang disediakan oleh modul. Kelas dapat mewakili zona waktu sederhana dengan offset tetap dari UTC, seperti UTC sendiri atau zona waktu EST dan EDT Amerika Utara. Mendukung zona waktu pada tingkat detail yang lebih dalam tergantung pada aplikasinya. Aturan penyesuaian waktu di seluruh dunia lebih politis daripada rasional, sering berubah, dan tidak ada standar yang cocok untuk setiap aplikasi selain UTC KonstantaModul mengekspor konstanta berikut waktu. MINYEARAngka tahun terkecil yang diperbolehkan dalam sebuah atau objek. adalah >>> # 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.05waktu. MAXYEAR Angka tahun terbesar yang diperbolehkan dalam sebuah atau objek. adalah >>> # 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_9waktu. UTC Alias untuk singleton zona waktu UTC Baru di versi 3. 11 Jenis yang Tersediakelas tanggal waktu. tanggalTanggal naif yang diidealkan, dengan asumsi kalender Gregorian saat ini selalu berlaku, dan akan selalu berlaku. Atribut. , , dan kelas tanggal waktu. waktuWaktu ideal, terlepas dari hari tertentu, dengan asumsi bahwa setiap hari memiliki tepat 24*60*60 detik. (Tidak ada gagasan "detik kabisat" di sini. ) Atribut. , , , , dan kelas tanggal waktu. tanggal waktuKombinasi tanggal dan waktu. Atribut. , , , , , , , dan kelas tanggal waktu. timedeltaDurasi yang menyatakan perbedaan antara dua , , atau instance ke resolusi mikrodetik kelas tanggal waktu. tzinfoKelas dasar abstrak untuk objek informasi zona waktu. Ini digunakan oleh kelas dan untuk memberikan gagasan penyesuaian waktu yang dapat disesuaikan (misalnya, untuk memperhitungkan zona waktu dan/atau waktu musim panas) kelas tanggal waktu. zona waktuKelas yang mengimplementasikan kelas dasar abstrak sebagai offset tetap dari UTC Baru di versi 3. 2 Objek jenis ini tidak dapat diubah Hubungan subkelas object timedelta tzinfo timezone time date datetime Properti UmumJenis , , , dan berbagi fitur umum ini
Menentukan apakah suatu Objek Sadar atau NaifObjek seperti itu selalu naif Objek tipe atau mungkin sadar atau naif Objek d mengetahui jika kedua hal berikut berlaku
Kalau tidak, d naif Objek t sadar jika kedua hal berikut berlaku
Kalau tidak, itu naif Perbedaan antara sadar dan naif tidak berlaku untuk objek ObjekObjek mewakili durasi, perbedaan antara dua tanggal atau waktu kelas tanggal waktu. timedelta(hari=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)Semua argumen bersifat opsional dan default ke >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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. Argumen mungkin bilangan bulat atau float, dan mungkin positif atau negatif Hanya hari, detik, dan mikrodetik yang disimpan secara internal. Argumen dikonversi ke unit tersebut
dan hari, detik, dan mikrodetik kemudian dinormalisasi sehingga representasi menjadi unik, dengan
Contoh berikut mengilustrasikan bagaimana argumen apa pun selain hari, detik, dan mikrodetik "digabungkan" dan dinormalisasi menjadi tiga atribut hasil tersebut >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)_ Jika ada argumen yang merupakan pelampung dan ada mikrodetik pecahan, mikrodetik pecahan yang tersisa dari semua argumen digabungkan dan jumlahnya dibulatkan ke mikrodetik terdekat menggunakan tiebreak putaran-setengah-ke-genap. Jika tidak ada argumen yang mengambang, proses konversi dan normalisasi tepat (tidak ada informasi yang hilang) Jika nilai hari yang dinormalisasi berada di luar rentang yang ditunjukkan, dinaikkan Perhatikan bahwa normalisasi nilai negatif mungkin mengejutkan pada awalnya. Sebagai contoh >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999) Atribut kelas delta waktu. mntObjek paling negatif, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)20delta waktu. maks Objek paling positif, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)22delta waktu. resolusi Perbedaan sekecil mungkin antara objek yang tidak sama, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Perhatikan bahwa, karena normalisasi, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 tidak dapat direpresentasikan sebagai objek Atribut instance (hanya baca) Atribut Nilai >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Antara -999999999 dan 999999999 inklusif >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Antara 0 dan 86399 inklusif >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Antara 0 dan 999999 inklusif Operasi yang didukung Operasi Hasil >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Jumlah t2 dan t3. Setelah itu t1-t2 == t3 dan t1-t3 == t2 benar. (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 Selisih t2 dan t3. Setelah itu t1 == t2 - t3 dan t2 == t1 + t3 benar. (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 dikalikan dengan bilangan bulat. Setelah itu t1 // i == t2 benar, asalkan >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Secara umum, t1 * i == t1 * (i-1) + t1 benar. (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 dikalikan dengan pelampung. Hasilnya dibulatkan ke kelipatan timedelta terdekat. resolusi menggunakan putaran-setengah-ke-genap >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Pembagian (3) durasi keseluruhan t2 dengan satuan interval t3. Mengembalikan objek >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 dibagi dengan float atau int. Hasilnya dibulatkan ke kelipatan timedelta terdekat. resolution using round-half-to-even >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)40 or >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)41 The floor is computed and the remainder (if any) is thrown away. In the second case, an integer is returned. (3) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)42 The remainder is computed as a object. (3) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)44 Computes the quotient and the remainder. >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)45 (3) and >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)46. q is an integer and r is a object >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)48 Returns a object with the same value. (2) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)50 equivalent to (-t1. days, -t1. seconds, -t1. microseconds), and to t1* -1. (1)(4) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)52 equivalent to +t when >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)53, and to -t when >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)54. (2) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)55 Returns a string in the form >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)56, where D is negative for negative >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)57. (5) >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)58 Returns a string representation of the object as a constructor call with canonical attribute values Notes
In addition to the operations listed above, objects support certain additions and subtractions with and objects (see below) Changed in version 3. 2. Floor division and true division of a object by another object are now supported, as are remainder operations and the function. True division and multiplication of a object by a object are now supported. Comparisons of objects are supported, with some caveats The comparisons >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)75 or >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)76 always return a , no matter the type of the compared object >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False For all other comparisons (such as >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 and >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)79), when a object is compared to an object of a different type, is raised >>> 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' In Boolean contexts, a object is considered to be true if and only if it isn’t equal 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)83 Instance methods timedelta. total_seconds()Return the total number of seconds contained in the duration. 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)84. For interval units other than seconds, use the division form directly (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) Note that for very large time intervals (greater than 270 years on most platforms) this method will lose microsecond accuracy Baru di versi 3. 2 Contoh penggunaan.Contoh tambahan normalisasi >>> # 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_ Contoh aritmatika >>> 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) ObjekObjek mewakili tanggal (tahun, bulan, dan hari) dalam kalender ideal, kalender Gregorian saat ini diperpanjang tanpa batas waktu di kedua arah 1 Januari tahun 1 disebut hari nomor 1, 2 Januari tahun 1 disebut hari nomor 2, dan seterusnya. kelas tanggal waktu. tanggal(tahun , bulan, day)Semua argumen diperlukan. Argumen harus berupa bilangan bulat, dalam rentang berikut
Jika argumen di luar rentang tersebut diberikan, akan dimunculkan Konstruktor lain, semua metode kelas metode kelas tanggal. hari ini()Mengembalikan tanggal lokal saat ini Ini setara dengan >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)_94metode kelas tanggal. fromtimestamp(timestamp) Kembalikan tanggal lokal yang sesuai dengan stempel waktu POSIX, seperti yang dikembalikan oleh Hal ini dapat meningkat, jika stempel waktu berada di luar rentang nilai yang didukung oleh fungsi platform C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97, dan pada kegagalan >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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. Ini umum untuk dibatasi pada tahun dari tahun 1970 hingga 2038. Perhatikan bahwa pada sistem non-POSIX yang menyertakan detik kabisat dalam gagasannya tentang stempel waktu, detik kabisat diabaikan oleh Berubah di versi 3. 3. Angkat alih-alih jika stempel waktu berada di luar rentang nilai yang didukung oleh fungsi platform C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97. Naikkan alih-alih pada >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 kegagalan. metode kelas tanggal. dariordinal(ordinal) Kembalikan tanggal yang sesuai dengan ordinal Gregorian proleptik, di mana 1 Januari tahun 1 memiliki ordinal 1 dinaikkan kecuali >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)_08. Untuk setiap tanggal d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)_09metode kelas tanggal. fromisoformat(date_string) Kembalikan yang sesuai dengan date_string yang diberikan dalam format ISO 8601 yang valid, kecuali tanggal ordinal (mis. 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) Baru di versi 3. 7 Berubah di versi 3. 11. Sebelumnya, metode ini hanya mendukung format >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)12. metode kelas tanggal. dari isocalendar(tahun , minggu, day) Kembalikan yang sesuai dengan tanggal kalender ISO yang ditentukan berdasarkan tahun, minggu, dan hari. Ini adalah kebalikan dari fungsi Baru di versi 3. 8 Atribut kelas date. minThe earliest representable date, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)15date. max The latest representable date, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)16date. resolution Perbedaan sekecil mungkin antara objek tanggal yang tidak sama, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)17 Atribut instance (hanya baca) date. yearBetween and inclusive date. monthBetween 1 and 12 inclusive date. dayBetween 1 and the number of days in the given month of the given year Operasi yang didukung Operasi Hasil >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)20 date2 will be >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)21 days after date1. (1) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)22 Computes date2 such that >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)23. (2) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)24 (3) >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)25 date1 is considered less than date2 when date1 precedes date2 in time. (4) Notes
In Boolean contexts, all objects are considered to be true Instance methods date. replace(year=self. year , month=self. month , day=self. day)Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified Example >>> from datetime import date >>> d = date(2002, 12, 31) >>> d.replace(day=26) datetime.date(2002, 12, 26)date. timetuple() Return a such as returned by The hours, minutes and seconds are 0, and the DST flag is -1 >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)52 is equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)0 where >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)53 is the day number within the current year starting with >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05 for January 1stdate. toordinal() Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any object d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)09date. weekday() Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)57, a Wednesday. See also date. isoweekday() Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)59, a Wednesday. Lihat juga ,date. isocalendar() Return a object with three components. >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)1, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)63 and >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)64 The ISO calendar is a widely used variant of the Gregorian calendar. The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004 >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)1 Changed in version 3. 9. Result changed from a tuple to a . date. isoformat()Return a string representing the date in ISO 8601 format, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)12 >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)2date. __str__() For a date d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)66 is equivalent to >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)67date. ctime() Return a string representing the date >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)3 >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)68 is equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)4 on platforms where the native C >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)69 function (which invokes, but which does not invoke) conforms to the C standarddate. strftime(format) Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. For a complete list of formatting directives, see date. __format__(format)Same as . This makes it possible to specify a format string for a object in and when using . For a complete list of formatting directives, see Examples of Usage.Example of counting days to an event >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)5 More examples of working with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)6 ObjekA object is a single object containing all the information from a object and a object Like a object, assumes the current Gregorian calendar extended in both directions; like a object, assumes there are exactly 3600*24 seconds in every day Constructor class datetime. datetime(year , month , day , hour=0 , minute=0 , second=0 , microsecond=0 , tzinfo=None , * , fold=0)Argumen tahun, bulan dan hari diperlukan. 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
Jika argumen di luar rentang tersebut diberikan, akan dimunculkan New in version 3. 6. Added the >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96 argument. Konstruktor lain, semua metode kelas metode kelas tanggal waktu. today()Return the current local datetime, with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 Equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)7 See also , Metode ini secara fungsional setara dengan , tetapi tanpa parameter >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0002classmethod datetime. now(tz=None) Return the current local date and time If optional argument tz is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or not specified, this is like , but, if possible, supplies more precision than can be gotten from going through a timestamp (for example, this may be possible on platforms supplying the C >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0006 function) If tz is not >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, it must be an instance of a subclass, and the current date and time are converted to tz’s time zone This function is preferred over and classmethod datetime. utcnow()Return the current UTC date and time, with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 This is like , but returns the current UTC date and time, as a naive object. An aware current UTC datetime can be obtained by calling >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0015. See also Warning Because naive >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 objects are treated by many >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0015classmethod datetime. fromtimestamp(timestamp , tz=None) Return the local date and time corresponding to the POSIX timestamp, such as is returned by . If optional argument tz is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or not specified, the timestamp is converted to the platform’s local date and time, and the returned object is naive If tz is not >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, it must be an instance of a subclass, and the timestamp is converted to tz’s time zone may raise , if the timestamp is out of the range of values supported by the platform C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 or >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 functions, and on >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 or >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 failure. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by , and then it’s possible to have two timestamps differing by a second that yield identical objects. This method is preferred over Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 or >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 functions. Raise instead of on >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)97 or >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 failure. Changed in version 3. 6. may return instances with set to 1. classmethod datetime. utcfromtimestamp(timestamp)Return the UTC corresponding to the POSIX timestamp, with >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. (The resulting object is naive. ) This may raise , if the timestamp is out of the range of values supported by the platform C >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 function, and on >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 failure. It’s common for this to be restricted to years in 1970 through 2038 To get an aware object, call >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)8 On the POSIX compliant platforms, it is equivalent to the following expression >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)9 except the latter formula always supports the full years range. between and inclusive Warning Because naive >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 objects are treated by many >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0058 Changed in version 3. 3. Raise instead of if the timestamp is out of the range of values supported by the platform C >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 function. Raise instead of on >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0028 failure. classmethod datetime. fromordinal(ordinal) Return the corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. is raised unless >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0067. The hour, minute, second and microsecond of the result are all 0, and is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03classmethod datetime. combine(date , time , tzinfo=self. tzinfo) Return a new object whose date components are equal to the given object’s, and whose time components are equal to the given object’s. Jika argumen tzinfo disediakan, nilainya digunakan untuk mengatur atribut hasil, jika tidak, atribut argumen waktu digunakan For any object d, >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0076. If date is a object, its time components and attributes are ignored Changed in version 3. 6. Added the tzinfo argument. classmethod datetime. fromisoformat(date_string)Return a corresponding to a date_string in any valid ISO 8601 format, with the following exceptions
Examples >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)0 Baru di versi 3. 7 Changed in version 3. 11. Previously, this method only supported formats that could be emitted by or . classmethod datetime. fromisocalendar(year , week , day)Return a corresponding to the ISO calendar date specified by year, week and day. The non-date components of the datetime are populated with their normal default values. This is the inverse of the function Baru di versi 3. 8 classmethod datetime. strptime(date_string , format)Return a corresponding to date_string, parsed according to format This is equivalent to >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)1 is raised if the date_string and format can’t be parsed by or if it returns a value which isn’t a time tuple. For a complete list of formatting directives, see Atribut kelas datetime. minThe earliest representable , >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0089datetime. max The latest representable , >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0091datetime. resolution Perbedaan sekecil mungkin antara objek yang tidak sama, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 Atribut instance (hanya baca) datetime. yearBetween and inclusive datetime. monthBetween 1 and 12 inclusive datetime. dayBetween 1 and the number of days in the given month of the given year datetime. hourIn >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0096waktu. minute In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097datetime. second In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097datetime. microsecond In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0099 The object passed as the tzinfo argument to the constructor, or >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 if none was passeddatetime. fold In >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False02. Digunakan untuk membedakan waktu dinding selama interval berulang. (A repeated interval occurs when clocks are rolled back at the end of daylight saving time or when the UTC offset for the current zone is decreased for political reasons. ) The value 0 (1) represents the earlier (later) of the two moments with the same wall time representation New in version 3. 6 Operasi yang didukung Operasi Hasil >>> 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 Compares to . (4)
Instance methods datetime. date()Return object with same year, month and day datetime. time()Return object with same hour, minute, second, microsecond and fold. is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. See also method Changed in version 3. 6. The fold value is copied to the returned object. datetime. timetz()Return object with same hour, minute, second, microsecond, fold, and tzinfo attributes. See also method Changed in version 3. 6. The fold value is copied to the returned object. datetime. replace(year=self. year , month=self. month , day=self. day , hour=self. hour , minute=self. minute , second=self. second , microsecond=self. microsecond , tzinfo=self. tzinfo , * , lipat=0)Return a datetime with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note that >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False52 can be specified to create a naive datetime from an aware datetime with no conversion of date and time data New in version 3. 6. Added the >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96 argument. datetime. astimezone(tz=None) Return a object with new attribute tz, adjusting the date and time data so the result is the same UTC time as self, but in tz’s local time If provided, tz must be an instance of a subclass, and its and methods must not return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. If self is naive, it is presumed to represent time in the system timezone If called without arguments (or with >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False60) the system local timezone is assumed for the target timezone. The >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False61 attribute of the converted datetime instance will be set to an instance of with the zone name and offset obtained from the OS If >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False63 is tz, >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False64 is equal to self. no adjustment of date or time data is performed. Else the result is local time in the timezone tz, representing the same UTC time as self. after >>> 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 will have the same date and time data as >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False67 If you merely want to attach a time zone object tz to a datetime dt without adjustment of date and time data, use >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False68. If you merely want to remove the time zone object from an aware datetime dt without conversion of date and time data, use >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 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 Changed in version 3. 3. tz now can be omitted. Changed in version 3. 6. The method can now be called on naive instances that are presumed to represent system local time. datetime. utcoffset()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False78, and raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or a object with magnitude less than one day Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. datetime. dst()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False84, and raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or a object with magnitude less than one day Changed in version 3. 7. The DST offset is not restricted to a whole number of minutes. datetime. tzname()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False90, raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or a string object,waktu. timetuple() Return a such as returned by >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)52 is equivalent to >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)3 where >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)53 is the day number within the current year starting with >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05 for January 1st. The >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 flag of the result is set according to the method. is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 is set to >>> 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; else if returns a non-zero value, >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 is set to >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.05; else >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 is set to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)14datetime. 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 is never in effect for a UTC time If d is aware, d is normalized to UTC time, by subtracting >>> 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, and a for the normalized time is returned. >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 is forced to 0. Note that an may be raised if d. tahun adalah >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.0_4 atau >>> # 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 dan penyesuaian UTC melampaui batas tahun Warning Karena objek >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 yang naif diperlakukan oleh banyak metode >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 sebagai waktu lokal, lebih disukai menggunakan waktu yang diketahui untuk mewakili waktu dalam UTC; . Jika Anda memiliki >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False7 naif yang mewakili UTC, gunakan >>> 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 untuk membuatnya sadar, pada titik mana Anda dapat menggunakanwaktu. toordinal() Kembalikan ordinal Gregorian proleptik dari tanggal tersebut. Sama seperti >>> 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'_26waktu. stempel waktu() Kembalikan stempel waktu POSIX yang sesuai dengan instance. Nilai yang dikembalikan mirip dengan yang dikembalikan oleh Contoh naif diasumsikan mewakili waktu lokal dan metode ini bergantung pada fungsi platform 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 untuk melakukan konversi. Karena mendukung rentang nilai yang lebih luas daripada >>> 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 pada banyak platform, metode ini dapat meningkatkan waktu jauh di masa lalu atau jauh di masa depan For aware instances, the return value is computed as >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)_4 Baru di versi 3. 3 Berubah di versi 3. 6. Metode ini menggunakan atribut untuk membedakan waktu selama interval berulang. Note Tidak ada metode untuk mendapatkan stempel waktu POSIX langsung dari instance naif yang mewakili waktu UTC. Jika aplikasi Anda menggunakan konvensi ini dan zona waktu sistem Anda tidak disetel ke UTC, Anda dapat memperoleh stempel waktu POSIX dengan menyediakan >>> 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 atau dengan menghitung stempel waktu secara langsung >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)6waktu. hari kerja() Mengembalikan hari dalam seminggu sebagai bilangan bulat, di mana Senin adalah 0 dan Minggu adalah 6. Sama dengan >>> 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. Lihat jugawaktu. hari kerja iso() Mengembalikan hari dalam seminggu sebagai bilangan bulat, di mana Senin adalah 1 dan Minggu adalah 7. Sama seperti >>> 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. Lihat juga ,waktu. isokalendar() Kembalikan a dengan tiga komponen. >>> 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 dan >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)64. Sama seperti >>> 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'_48waktu. isoformat(sep=', timespec='auto') Kembalikan string yang mewakili tanggal dan waktu dalam format ISO 8601
Jika tidak mengembalikan >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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, sebuah string ditambahkan, memberikan offset UTC
Examples >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)7 Argumen opsional sep (default >>> 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) adalah pemisah satu karakter, ditempatkan di antara bagian tanggal dan waktu dari hasil. Sebagai contoh >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)8 Argumen opsional timespec menentukan jumlah komponen tambahan waktu yang akan disertakan (defaultnya adalah >>> 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). Itu bisa menjadi salah satu dari yang berikut
Note Komponen waktu yang dikecualikan dipotong, bukan dibulatkan akan dimunculkan pada argumen timespec yang tidak valid >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)_9 Baru di versi 3. 6. Menambahkan argumen timespec. waktu. __str__()Misalnya d, >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)_66 setara dengan >>> 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'84waktu. waktu() Kembalikan string yang mewakili tanggal dan waktu >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:00_0 String keluaran tidak akan menyertakan informasi zona waktu, terlepas dari apakah masukannya sadar atau naif >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)68 is equivalent to >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)4 on platforms where the native C >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)69 function (which invokes, but which does not invoke) conforms to the C standardwaktu. strftime(format) Return a string representing the date and time, controlled by an explicit format string. Untuk daftar lengkap arahan pemformatan, lihat waktu. __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.Contoh bekerja dengan objek >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:00_2 Contoh di bawah menentukan subkelas yang menangkap informasi zona waktu untuk Kabul, Afghanistan, yang menggunakan +4 UTC hingga tahun 1945 dan kemudian +4. 30 UTC sesudahnya >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:00_3 Penggunaan >>> 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 dari atas >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:00_4 ObjekObjek mewakili waktu (lokal) dalam sehari, terlepas dari hari tertentu, dan dapat disesuaikan melalui objek kelas tanggal waktu. waktu(jam=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)Semua argumen bersifat opsional. tzinfo mungkin >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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, atau turunan dari subkelas. Argumen yang tersisa harus berupa bilangan bulat dalam rentang berikut
Jika argumen di luar rentang tersebut diberikan, akan dimunculkan. Semua default ke >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 kecuali tzinfo, yang defaultnya ke Atribut kelas waktu. mntTerwakili paling awal , >>> # 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_10waktu. maks Perwakilan terbaru, >>> # 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_12waktu. resolusi Perbedaan sekecil mungkin antara objek yang tidak sama, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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, meskipun perhatikan bahwa aritmatika pada objek tidak didukung Atribut instance (hanya baca) waktu. jamIn >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0096waktu. menit In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097waktu. detik In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0097waktu. mikrodetik In >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:0099waktu. tzinfo The object passed as the tzinfo argument to the constructor, or >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 if none was passedwaktu. lipat In >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False02. Digunakan untuk membedakan waktu dinding selama interval berulang. (A repeated interval occurs when clocks are rolled back at the end of daylight saving time or when the UTC offset for the current zone is decreased for political reasons. ) The value 0 (1) represents the earlier (later) of the two moments with the same wall time representation New in version 3. 6 objek mendukung perbandingan ke , di mana a dianggap kurang dari b saat a mendahului b dalam waktu. Jika satu pembanding naif dan yang lain sadar, dimunculkan jika perbandingan pesanan dicoba. Untuk perbandingan kesetaraan, contoh naif tidak pernah sama dengan contoh sadar Jika kedua perbandingan diketahui, dan memiliki atribut yang sama, atribut umum diabaikan dan waktu dasar dibandingkan. Jika kedua komparand sadar dan memiliki atribut yang berbeda, komparand disesuaikan terlebih dahulu dengan mengurangkan offset UTC-nya (diperoleh dari >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False30). Untuk menghentikan perbandingan tipe campuran agar tidak kembali ke perbandingan default berdasarkan alamat objek, saat objek dibandingkan dengan objek dari tipe yang berbeda, dimunculkan kecuali jika perbandingannya adalah >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 atau >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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. Kasus terakhir kembali atau , masing-masing Changed in version 3. 3. Equality comparisons between aware and naive instances don’t raise . Dalam konteks Boolean, sebuah objek selalu dianggap benar Changed in version 3. 5. Before Python 3. 5, sebuah objek dianggap salah jika mewakili tengah malam di UTC. This behavior was considered obscure and error-prone and has been removed in Python 3. 5. See bpo-13936 for full details. Other constructor classmethod time. fromisoformat(time_string)Kembalikan yang sesuai dengan time_string dalam format ISO 8601 yang valid, dengan pengecualian berikut
Examples >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:005 Baru di versi 3. 7 Changed in version 3. 11. Previously, this method only supported formats that could be emitted by . Instance methods time. replace(hour=self. hour , minute=self. minute , second=self. second , microsecond=self. microsecond , tzinfo=self. tzinfo , * , fold=0)Return a with the same value, except for those attributes given new values by whichever keyword arguments are specified. Note that >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False52 can be specified to create a naive from an aware , without conversion of the time data New in version 3. 6. Added the >>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)96 argument. time. isoformat(timespec='auto') Return a string representing the time in ISO 8601 format, one of
Argumen opsional timespec menentukan jumlah komponen tambahan waktu yang akan disertakan (defaultnya adalah >>> 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). Itu bisa menjadi salah satu dari yang berikut
Note Komponen waktu yang dikecualikan dipotong, bukan dibulatkan will be raised on an invalid timespec argument Example >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:006 Baru di versi 3. 6. Menambahkan argumen timespec. time. __str__()For a time t, >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)55 is equivalent to >>> # Components of another_year add up to exactly 365 days >>> from datetime import timedelta >>> year = timedelta(days=365) >>> another_year = timedelta(weeks=40, days=84, hours=23, .. minutes=50, seconds=600) >>> year == another_year True >>> year.total_seconds() 31536000.083time. strftime(format) Return a string representing the time, controlled by an explicit format string. For a complete list of formatting directives, see time. __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 time. utcoffset()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> # 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, and raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or a object with magnitude less than one day Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. time. dst()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> # 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, and raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, or a object with magnitude less than one day Changed in version 3. 7. The DST offset is not restricted to a whole number of minutes. time. tzname()If is >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, returns >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, else returns >>> from datetime import timedelta >>> 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, or raises an exception if the latter doesn’t return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 or a string object Examples of Usage.Examples of working with a object >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:007 Objekkelas tanggal waktu. tzinfoThis is an abstract base class, meaning that this class should not be instantiated directly. Define a subclass of to capture information about a particular time zone An instance of (a concrete subclass of) can be passed to the constructors for and objects. The latter objects view their attributes as being in local time, and the object supports methods revealing offset of local time from UTC, the name of the time zone, and DST offset, all relative to a date or time object passed to them You need to derive a concrete subclass, and (at least) supply implementations of the standard methods needed by the methods you use. The module provides , a simple concrete subclass of which can represent timezones with fixed offset from UTC such as UTC itself or North American EST and EDT Special requirement for pickling. A subclass must have an >>> 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 method that can be called with no arguments, otherwise it can be pickled but possibly not unpickled again. This is a technical requirement that may be relaxed in the future A concrete subclass of may need to implement the following methods. Exactly which methods are needed depends on the uses made of aware objects. If in doubt, simply implement all of them tzinfo. utcoffset(dt)Return offset of local time from UTC, as a object that is positive east of UTC. If local time is west of UTC, this should be negative This represents the total offset from UTC; for example, if a object represents both time zone and DST adjustments, should return their sum. If the UTC offset isn’t known, return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03. Else the value returned must be a object 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 (the magnitude of the offset must be less than one day). Most implementations of will probably look like one of these two >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:008 If does not return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03, should not return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 either The default implementation of raises Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes. tzinfo. dst(dt)Return the daylight saving time (DST) adjustment, as a object or >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 if DST information isn’t known Return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)83 if DST is not in effect. If DST is in effect, return the offset as a object (see for details). Note that DST offset, if applicable, has already been added to the UTC offset returned by , so there’s no need to consult unless you’re interested in obtaining DST info separately. For example, calls its attribute’s method to determine how the >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False97 flag should be set, and calls to account for DST changes when crossing time zones An instance tz of a subclass that models both standard and daylight times must be consistent in this sense >>> 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 must return the same result for every dt 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)51 For sane subclasses, this expression yields the time zone’s “standard offset”, which should not depend on the date or the time, but only on geographic location. The implementation of relies on this, but cannot detect violations; it’s the programmer’s responsibility to ensure it. If a subclass cannot guarantee this, it may be able to override the default implementation of to work correctly with >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False72 regardless Most implementations of will probably look like one of these two >>> timedelta(hours=-5) datetime.timedelta(days=-1, seconds=68400) >>> print(_) -1 day, 19:00:009 or >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False0 The default implementation of raises Changed in version 3. 7. The DST offset is not restricted to a whole number of minutes. tzinfo. tzname(dt)Return the time zone name corresponding to the object dt, as a string. Nothing about string names is defined by the module, and there’s no requirement that it mean anything in particular. For example, “GMT”, “UTC”, “-500”, “-5. 00”, “EDT”, “US/Eastern”, “America/New York” are all valid replies. Return >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 if a string name isn’t known. Note that this is a method rather than a fixed string primarily because some subclasses will wish to return different names depending on the specific value of dt passed, especially if the class is accounting for daylight time The default implementation of raises These methods are called by a or object, in response to their methods of the same names. A object passes itself as the argument, and a object passes >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 as the argument. A subclass’s methods should therefore be prepared to accept a dt argument of >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 of class When >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=10, .. milliseconds=29000, .. minutes=5, .. hours=8, .. weeks=2 .. ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)03 is passed, it’s up to the class designer to decide the best response. For example, returning >>> from datetime import timedelta >>> delta = timedelta( .. days=50, .. seconds=27, .. microseconds=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 is appropriate if the class wishes to say that time objects don’t participate in the protocols. It may be more useful for >>> 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 to return the standard UTC offset, as there is no other convention for discovering the standard offset When a object is passed in response to a method, >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)81 is the same object as self. methods can rely on this, unless user code calls methods directly. The intent is that the methods interpret dt as being in local time, and not need worry about objects in other timezones There is one more method that a subclass may wish to override tzinfo. fromutc(dt)This is called from the default implementation. When called from that, >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)81 is self, and dt’s date and time data are to be viewed as expressing a UTC time. The purpose of is to adjust the date and time data, returning an equivalent datetime in self’s local time Most subclasses should be able to inherit the default implementation without problems. It’s strong enough to handle fixed-offset time zones, and time zones accounting for both standard and daylight time, and the latter even if the DST transition times differ in different years. An example of a time zone the default implementation may not handle correctly in all cases is one where the standard offset (from UTC) depends on the specific date and time passed, which can happen for political reasons. The default implementations of >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False72 and may not produce the result you want if the result is one of the hours straddling the moment the standard offset changes Skipping code for error cases, the default implementation acts like >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False1 In the following >>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)95 file there are some examples of classes >>> from datetime import timedelta >>> delta1 = timedelta(seconds=57) >>> delta2 = timedelta(hours=25, seconds=2) >>> delta2 != delta1 True >>> delta2 == 5 False2 Perhatikan bahwa ada seluk-beluk yang tidak dapat dihindari dua kali per tahun dalam akuntansi subkelas untuk waktu standar dan siang hari, pada titik transisi DST. 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) pada hari Minggu pertama bulan 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. Waktu dinding dalam bentuk 2. MM tidak masuk akal pada hari itu, jadi >>> 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 tidak akan memberikan hasil dengan >>> 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 pada hari DST dimulai. 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 False_4 Saat DST berakhir ("garis akhir"), ada potensi masalah yang lebih buruk. ada satu jam yang tidak bisa dieja dengan jelas dalam waktu dinding lokal. the last hour of daylight time. Di Timur, itu adalah waktu dalam bentuk 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. Misalnya, pada transisi musim gugur tahun 2016, kami mendapatkan >>> 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)) Lihat juga 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 ObjekThe 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 Baru di versi 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 Atribut kelas 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 Example 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 Hari kerja sebagai nama lengkap lokal 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, …, Dezember (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 Month 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)68 Year without century as a zero-padded decimal number 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 Microsecond as a decimal number, zero-padded to 6 digits 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 dalam bentuk >>> from datetime import date >>> date.fromisoformat('2019-12-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 (string kosong jika objeknya naif) (kosong), +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 Example 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) These may not be available on all platforms when used with the >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)40 method. The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above. Calling >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)41 with incomplete or ambiguous ISO 8601 directives will raise a The full set of format codes supported varies across platforms, because Python calls the platform C library’s >>> from datetime import date >>> date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)40 function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. Ada juga perbedaan antar platform dalam menangani penentu format yang tidak didukung 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 Notes
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 datetime date in Python?Convert a String to a datetime object using datetime. strptime() The datetime. strptime() method returns a datetime object that matches the date_string parsed by the format. Both arguments are required and must be strings.
How to convert isoformat to datetime in Python?As pointed out by Martijn, if you created the datetime object using isoformat(), you can simply use datetime. fromisoformat() .
What is the Isoformat of date string in Python?Practical Data Science using Python
ISO 8601 is a date and time format which helps to remove different forms of the day, date, and time conventions across the world. To tackle this uncertainty of various formats ISO sets a format to represent dates “ YYYY-MM-DD ”. For example, May 31, 2022, is represented as 2022-05-31.
How do I convert datetime to Isoformat?The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i. e, in the form of (YYYY-MM-DDTHH. mm. ss. sssZ or ±YYYYYY-MM-DDTHH. mm. ss. |