Python mengonversi string ke isoformat datetime

Sementara aritmatika tanggal dan waktu didukung, fokus penerapannya adalah pada ekstraksi atribut yang efisien untuk pemformatan dan manipulasi output

Lihat juga

Modul

Fungsi terkait kalender umum

Modul

Akses waktu dan konversi

Modul

Zona waktu konkret yang mewakili basis data zona waktu IANA

Penggunaan tanggal paket

Pustaka pihak ketiga dengan zona waktu yang diperluas dan dukungan parsing

Objek Sadar dan Naif

Objek 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

Konstanta

Modul mengekspor konstanta berikut

waktu. MINYEAR

Angka 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.0
5

waktu. 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
_9

waktu. UTC

Alias ​​untuk singleton zona waktu UTC

Baru di versi 3. 11

Jenis yang Tersedia

kelas tanggal waktu. tanggal

Tanggal naif yang diidealkan, dengan asumsi kalender Gregorian saat ini selalu berlaku, dan akan selalu berlaku. Atribut. , , dan

kelas tanggal waktu. waktu

Waktu 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 waktu

Kombinasi tanggal dan waktu. Atribut. , , , , , , , dan

kelas tanggal waktu. timedelta

Durasi yang menyatakan perbedaan antara dua , , atau instance ke resolusi mikrodetik

kelas tanggal waktu. tzinfo

Kelas 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 waktu

Kelas 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 Umum

Jenis , , , dan berbagi fitur umum ini

  • Objek jenis ini tidak dapat diubah

  • Objek jenis ini bersifat hashable, artinya dapat digunakan sebagai kunci kamus

  • Objek jenis ini mendukung pengawetan yang efisien melalui modul

Menentukan apakah suatu Objek Sadar atau Naif

Objek seperti itu selalu naif

Objek tipe atau mungkin sadar atau naif

Objek d mengetahui jika kedua hal berikut berlaku

  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)
    
    _02 bukan
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=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

  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)
    
    _04 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

Kalau tidak, d naif

Objek t sadar jika kedua hal berikut berlaku

  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)
    
    _07 bukan
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=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

  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)
    
    _09 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

Kalau tidak, itu naif

Perbedaan antara sadar dan naif tidak berlaku untuk objek

Objek

Objek 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

  • Satu milidetik diubah menjadi 1000 mikrodetik

  • Satu menit diubah menjadi 60 detik

  • Satu jam diubah menjadi 3600 detik

  • Seminggu diubah menjadi 7 hari

dan hari, detik, dan mikrodetik kemudian dinormalisasi sehingga representasi menjadi unik, 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)
    
    _15

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

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

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. mnt

Objek 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)
20

delta 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)
22

delta 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

  1. This is exact but may overflow

  2. This is exact and cannot overflow

  3. Division by 0 raises

  4. -timedelta. max is not representable as a object

  5. String representations of objects are normalized similarly to their internal representation. This leads to somewhat unusual results for negative timedeltas. For example

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

  6. The 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)
    
    63 will always be equal to the 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)
    
    64 except when t3 is 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)
    
    25; in that case the former will produce a result while the latter will overflow

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)

Objek

Objek 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

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

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

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

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)
_94

metode 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)
_09

metode 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. min

The earliest representable date,

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

date. max

The latest representable date,

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

date. 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. year

Between and inclusive

date. month

Between 1 and 12 inclusive

date. day

Between 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

  1. date2 is moved forward in time if

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    26, or backward if
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    27. Afterward
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    28.
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    29 and
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    30 are ignored. is raised if
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    32 would be smaller than or larger than

  2. >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    29 and
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    30 are ignored

  3. This is exact, and cannot overflow. timedelta. seconds and timedelta. microseconds are 0, and date2 + timedelta == date1 after

  4. In other words,

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    25 if and only if
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    38. Date comparison raises if the other comparand isn’t also a object. However,
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    41 is returned instead if the other comparand has a
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    42 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when a object is compared to an object of a different type, is raised unless the comparison is
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    75 or
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    76. The latter cases return or , respectively

In Boolean contexts, all objects are considered to be true

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.0
5 for January 1st

date. 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)
09

date. 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)
2

date. __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)
67

date. 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 standard

date. 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

Objek

A object is a single object containing all the information from a object and a object

Like a object, assumes the current Gregorian calendar extended in both directions; like a object, assumes there are exactly 3600*24 seconds in every day

Constructor

class datetime. datetime(year , month , day , hour=0 , minute=0 , second=0 , microsecond=0 , tzinfo=None , * , fold=0)

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

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

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

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

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

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

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

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

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

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:00
02

classmethod 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:00
06 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:00
15. See also

Warning

Because naive

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 objects are treated by many
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 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:00
15

classmethod 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:00
28 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:00
28 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:00
28 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:00
28 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:00
28 function, and on
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 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
False
7 objects are treated by many
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
7 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:00
58

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:00
28 function. Raise instead of on
>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00
28 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:00
67. 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)
03

classmethod 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:00
76. 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

  1. Time zone offsets may have fractional seconds

  2. The

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    80 separator may be replaced by any single unicode character

  3. Ordinal dates are not currently supported

  4. Fractional hours and minutes are not supported

Examples

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

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. min

The earliest representable ,

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

datetime. max

The latest representable ,

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

datetime. 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. year

Between and inclusive

datetime. month

Between 1 and 12 inclusive

datetime. day

Between 1 and the number of days in the given month of the given year

datetime. hour

In

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

waktu. minute

In

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

datetime. second

In

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

datetime. microsecond

In

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

waktu. 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 passed

datetime. fold

In

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. 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
False
03

(1)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
04

(2)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
05

(3)

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
06

Compares to . (4)

  1. datetime2 is a duration of timedelta removed from datetime1, moving forward in time if

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    21 > 0, or backward if
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    21 < 0. The result has the same attribute as the input datetime, and datetime2 - datetime1 == timedelta after. is raised if datetime2. year would be smaller than or larger than . Note that no time zone adjustments are done even if the input is an aware object

  2. Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same attribute as the input datetime, and no time zone adjustments are done even if the input is aware

  3. Subtraction of a from a is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive, is raised

    If both are naive, or both are aware and have the same attribute, the attributes are ignored, and the result is a object t such that

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    22. No time zone adjustments are done in this case

    If both are aware and have different attributes,

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    24 acts as if a and b were first converted to naive UTC datetimes first. The result is
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    25 except that the implementation never overflows

  4. datetime1 is considered less than datetime2 when datetime1 precedes datetime2 in time

    If one comparand is naive and the other is aware, is raised if an order comparison is attempted. For equality comparisons, naive instances are never equal to aware instances

    If both comparands are aware, and have the same attribute, the common attribute is ignored and the base datetimes are compared. If both comparands are aware and have different attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    30)

    Changed in version 3. 3. Equality comparisons between aware and naive instances don’t raise .

    Note

    In order to stop comparison from falling back to the default scheme of comparing object addresses, datetime comparison normally raises if the other comparand isn’t also a object. However,

    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    41 is returned instead if the other comparand has a
    >>> from datetime import timedelta
    >>> d = timedelta(microseconds=-1)
    >>> (d.days, d.seconds, d.microseconds)
    (-1, 86399, 999999)
    
    42 attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when a object is compared to an object of a different type, is raised unless the comparison is
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    75 or
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    76. The latter cases return or , respectively

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
False
52 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
False
60) 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
False
61 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
False
63 is tz,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
64 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
False
65,
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
66 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
False
67

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
False
68. 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
False
69

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
False
78, 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
False
84, 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
False
90, 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.0
5 for January 1st. The
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 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
False
97 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
False
97 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.0
5; else
>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
97 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)
14

datetime. 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
False
97 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
False
97 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.0
8 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
False
7 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
False
7 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
False
7 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 menggunakan

waktu. 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'
_26

waktu. 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)
6

waktu. 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 juga

waktu. 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'
_48

waktu. isoformat(sep=', timespec='auto')

Kembalikan string yang mewakili tanggal dan waktu dalam format ISO 8601

  • >>> 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'
    
    _49, jika bukan 0

  • >>> 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'
    
    _51, jika 0

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

  • >>> 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'
    
    _55, jika bukan 0

  • >>> 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'
    
    _57, jika 0

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

  • >>> 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. 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'
    
    _62 jika 0, 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'
    
    64 jika tidak

  • >>> 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'
    
    _65. Sertakan the dalam format ________0______67 dua digit

  • >>> 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'
    
    _68. Sertakan dan dalam format
    >>> 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'
    
    _71

  • >>> 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'
    
    _62. Sertakan , , dan dalam format
    >>> 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'
    
    76

  • >>> 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'
    
    _77. Sertakan waktu penuh, tetapi potong bagian kedua pecahan menjadi milidetik.
    >>> 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'
    
    _78

  • >>> 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'
    
    _64. Sertakan purna waktu dalam format
    >>> 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'
    
    80

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'
84

waktu. 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 standard

waktu. 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

Objek

Objek 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

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

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

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

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

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

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. mnt

Terwakili 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
_10

waktu. 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
_12

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, meskipun perhatikan bahwa aritmatika pada objek tidak didukung

Atribut instance (hanya baca)

waktu. jam

In

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

waktu. menit

In

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

waktu. detik

In

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

waktu. mikrodetik

In

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

waktu. 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 passed

waktu. lipat

In

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
02. 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
False
30). 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

  1. Time zone offsets may have fractional seconds

  2. The leading

    >>> timedelta(hours=-5)
    datetime.timedelta(days=-1, seconds=68400)
    >>> print(_)
    -1 day, 19:00:00
    
    80, normally required in cases where there may be ambiguity between a date and a time, is not required

  3. Fractional seconds may have any number of digits (anything beyond 6 will be truncated)

  4. Fractional hours and minutes are not supported

Examples

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

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
False
52 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

  • >>> 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'
    
    80, if is not 0

  • >>> 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'
    
    76, if is 0

  • >>> # Components of another_year add up to exactly 365 days
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> another_year = timedelta(weeks=40, days=84, hours=23,
    ..                          minutes=50, seconds=600)
    >>> year == another_year
    True
    >>> year.total_seconds()
    31536000.0
    
    53, 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

  • >>> # Components of another_year add up to exactly 365 days
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> another_year = timedelta(weeks=40, days=84, hours=23,
    ..                          minutes=50, seconds=600)
    >>> year == another_year
    True
    >>> year.total_seconds()
    31536000.0
    
    56, if is 0 and 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

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

  • >>> 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. 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'
    
    _62 jika 0, 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'
    
    64 jika tidak

  • >>> 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'
    
    _65. Sertakan the dalam format ________0______67 dua digit

  • >>> 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'
    
    _68. Sertakan dan dalam format
    >>> 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'
    
    _71

  • >>> 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'
    
    _62. Sertakan , , dan dalam format
    >>> 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'
    
    76

  • >>> 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'
    
    _77. Sertakan waktu penuh, tetapi potong bagian kedua pecahan menjadi milidetik.
    >>> 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'
    
    _78

  • >>> 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'
    
    _64. Sertakan purna waktu dalam format
    >>> 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'
    
    80

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:00
6

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.0
83

time. 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.0
90, 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.0
96, 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:00
7

Objek

kelas tanggal waktu. tzinfo

This 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:00
8

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
False
97 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
False
72 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:00
9

or

>>> from datetime import timedelta
>>> delta1 = timedelta(seconds=57)
>>> delta2 = timedelta(hours=25, seconds=2)
>>> delta2 != delta1
True
>>> delta2 == 5
False
0

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
False
72 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
False
1

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
False
2

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
False
3

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
False
72 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
False
5

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

The module has a basic class (for handling arbitrary fixed offsets from UTC) and its attribute (a UTC timezone instance)

>>> delta2 > delta1
True
>>> delta2 > 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'
1 brings the IANA timezone database (also known as the Olson database) to Python, and its usage is recommended

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

Objek

The class is a subclass of , each instance of which represents a timezone defined by a fixed offset from UTC

Objects of this class cannot be used to represent timezone information in the locations where different offsets are used in different days of the year or where historical changes have been made to civil time

class datetime. timezone(offset , name=None)

The offset argument must be specified as a object representing the difference between the local time and UTC. It must be strictly between

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
26 and
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> ten_years = 10 * year
>>> ten_years
datetime.timedelta(days=3650)
>>> ten_years.days // 365
10
>>> nine_years = ten_years - year
>>> nine_years
datetime.timedelta(days=3285)
>>> three_years = nine_years // 3
>>> three_years, three_years.days // 365
(datetime.timedelta(days=1095), 3)
27, otherwise is raised

The name argument is optional. If specified it must be a string that will be used as the value returned by the method

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)
03

timezone. 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. utc

The 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 Codes

The following is a list of all the format codes that the 1989 C standard requires, and these work on all platforms with a standard C implementation

Directive

Meaning

Example

Notes

>>> 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 Detail

Broadly 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.0
5 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

  1. Because the format depends on the current locale, care should be taken when making assumptions about the output value. Pengurutan bidang akan bervariasi (misalnya, "bulan/hari/tahun" versus "hari/bulan/tahun"), dan hasilnya mungkin berisi karakter Unicode yang dikodekan menggunakan penyandian default lokal (misalnya, jika lokal saat ini adalah

    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    16,

  2. 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)
    
    41 method can parse years in the full [1, 9999] range, but years < 1000 must be zero-filled to 4-digit width

    Changed in version 3. 2. In previous versions,

    >>> 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 was restricted to years >= 1900.

    Changed in version 3. 3. In version 3. 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)
    
    40 method was restricted to years >= 1000.

  3. 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)
    
    41 method, 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)
    
    72 directive only affects the output hour field if 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)
    
    71 directive is used to parse the hour

  4. Unlike the module, the module does not support leap seconds

  5. 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)
    
    41 method, 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)
    
    75 directive accepts from one to six digits and zero pads on the right.
    >>> 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 is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available)

  6. For a naive object, 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)
    
    76 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)
    
    78 format codes are replaced by empty strings

    For an aware object

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    76

    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57 is transformed into a string of the form
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    77, where
    >>> 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'
    
    67 is a 2-digit string giving the number of UTC offset hours,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    38 is a 2-digit string giving the number of UTC offset minutes,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    39 is a 2-digit string giving the number of UTC offset seconds and
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 is a 6-digit string giving the number of UTC offset microseconds. The
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 part is omitted when the offset is a whole number of seconds and both the
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    40 and the
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    39 part is omitted when the offset is a whole number of minutes. For example, if
    >>> from datetime import timedelta
    >>> delta1 = timedelta(seconds=57)
    >>> delta2 = timedelta(hours=25, seconds=2)
    >>> delta2 != delta1
    True
    >>> delta2 == 5
    False
    
    57 returns
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    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)
    
    76 is replaced with the string
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    47

    Changed in version 3. 7. The UTC offset is not restricted to a whole number of minutes.

    Changed in version 3. 7. When 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)
    
    76 directive is provided to 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)
    
    41 method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    50 will be parsed as an offset of one hour. In addition, providing
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    51 is identical to
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    52.

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78

    In

    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    40,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    78 is replaced by an empty string if
    >>> from datetime import timedelta
    >>> year = timedelta(days=365)
    >>> ten_years = 10 * year
    >>> ten_years
    datetime.timedelta(days=3650)
    >>> ten_years.days // 365
    10
    >>> nine_years = ten_years - year
    >>> nine_years
    datetime.timedelta(days=3285)
    >>> three_years = nine_years // 3
    >>> three_years, three_years.days // 365
    (datetime.timedelta(days=1095), 3)
    
    65 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; otherwise
    >>> 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 is replaced by the returned value, which must be a string

    >>> 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 only accepts certain values for
    >>> 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

    1. any value in

      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      61 for your machine’s locale

    2. the hard-coded values

      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      62 and
      >>> from datetime import date
      >>> d = date(2002, 12, 31)
      >>> d.replace(day=26)
      datetime.date(2002, 12, 26)
      
      63

    So someone living in Japan may have

    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    64,
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    62, and
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    63 as valid values, but probably not
    >>> from datetime import date
    >>> d = date(2002, 12, 31)
    >>> d.replace(day=26)
    datetime.date(2002, 12, 26)
    
    67. It will raise
    >>> from datetime import timedelta
    >>> delta = timedelta(
    ..     days=50,
    ..     seconds=27,
    ..     microseconds=10,
    ..     milliseconds=29000,
    ..     minutes=5,
    ..     hours=8,
    ..     weeks=2
    .. )
    >>> # Only days, seconds, and microseconds remain
    >>> delta
    datetime.timedelta(days=64, seconds=29156, microseconds=10)
    
    93 for invalid values

    Changed in version 3. 2. When 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)
    
    76 directive is provided to 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)
    
    41 method, an aware object will be produced. The
    >>> 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 of the result will be set to a instance.

  7. 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)
    
    41 method,
    >>> 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 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)
    
    81 are only used in calculations when the day of the week and the calendar year (
    >>> 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) are specified

  8. Similar 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)
    
    80 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)
    
    81,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    88 is only used in calculations when the day of the week and the ISO year (
    >>> 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) are specified in 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)
    
    41 format string. Also note that
    >>> 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 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)
    
    69 are not interchangeable

  9. 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)
    
    41 method, the leading zero is optional for formats
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    64,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    67,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    70,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    71,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    73,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    74,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    79,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    80,
    >>> from datetime import date
    >>> date.fromisoformat('2019-12-04')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('20191204')
    datetime.date(2019, 12, 4)
    >>> date.fromisoformat('2021-W01-1')
    datetime.date(2021, 1, 4)
    
    81, 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. 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)
    
    68 does require a leading zero

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.