* **`'fold=1'` means the second pass in case of time jumping back for one hour.**
* **Timedelta normalizes arguments to ±days, seconds (<86400)andmicroseconds(<1M).**
* **Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.**
* **`'<DTa> = resolve_imaginary(<DTa>)'` fixes DTs that fall into the missing hour.**
### Now
```python
<D/DTn> = D/DT.today() # Current local date or naive datetime.
<DTn> = DT.utcnow() # Naive datetime from current UTC time.
<DTa> = DT.now(<tzinfo>) # Aware datetime from current tz time.
<D/DTn> = D/DT.today() # Current local date or naive DT. Also DT.now().
<DTa> = DT.now(<tzinfo>) # Aware DT from current time in passed timezone.
```
* **To extract time use `'<DTn>.time()'`, `'<DTa>.time()'` or `'<DTa>.timetz()'`.**
### Timezone
```python
<tzinfo> = timezone.utc # London without daylight saving time.
<tzinfo> = timezone.utc # London without daylight saving time (DST).
<tzinfo> = timezone(<timedelta>) # Timezone with fixed offset from UTC.
<tzinfo> = tzlocal() # Local timezone. Also gettz().
<tzinfo> = gettz('<Continent>/<City>') # 'Continent/City_Name' timezone or None.
<DTa> = <DT>.astimezone([<tzinfo>]) # Converts DT to the passed or local timezone.
<DTa> = <DT>.astimezone([<tzinfo>]) # Converts DT to the passed or local fixed zone.
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Changes object's timezone without conversion.
```
* **Timezones returned by gettz(), tzlocal(), and implicit local timezone of naive objects have offsets that vary through time due to DST and historical changes of the zone's base offset.**
* **Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.**
>>> dt.strftime("%dth of %B '%y (%a), %I:%M%p %Z")
"14th of August '25 (Thu), 11:39PM UTC+02:00"
```
* **`'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` or empty string if datetime is naive.**
* **`'%Z'` accepts `'UTC/GMT'` and local timezone's code and returns timezone's name, `'UTC[±HH:MM]'` if timezone is nameless, or an empty string if datetime is naive.**
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
### Arithmetics
```python
<D/DT> = <D/DT> ± <TD> # Returned datetime can fall into missing hour.
<TD> = <D/DTn> - <D/DTn> # Returns the difference. Ignores time jumps.
<TD> = <DTa> - <DTa> # Ignores time jumps if they share tzinfo object.
<div><h2id="datetime"><ahref="#datetime"name="datetime">#</a>Datetime</h2><p><strong>Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># pip3 install python-dateutil</span>
<spanclass="hljs-keyword">from</span> datetime <spanclass="hljs-keyword">import</span> date, time, datetime, timedelta, timezone
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'fold=1'</span></code> means the second pass in case of time jumping back for one hour.</strong></li>
<li><strong>Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'<D/DT>.weekday()'</span></code> to get the day of the week as an int, with Monday being 0.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<DTa> = resolve_imaginary(<DTa>)'</span></code> fixes DTs that fall into the missing hour.</strong></li>
</ul>
<div><h3id="now">Now</h3><pre><codeclass="python language-python hljs"><D/DTn> = D/DT.today() <spanclass="hljs-comment"># Current local date or naive datetime.</span>
<DTn> = DT.utcnow() <spanclass="hljs-comment"># Naive datetime from current UTC time.</span>
<DTa> = DT.now(<tzinfo>) <spanclass="hljs-comment"># Aware datetime from current tz time.</span>
<div><h3id="now">Now</h3><pre><codeclass="python language-python hljs"><D/DTn> = D/DT.today() <spanclass="hljs-comment"># Current local date or naive DT. Also DT.now().</span>
<DTa> = DT.now(<tzinfo>) <spanclass="hljs-comment"># Aware DT from current time in passed timezone.</span>
</code></pre></div>
<ul>
<li><strong>To extract time use <codeclass="python hljs"><spanclass="hljs-string">'<DTn>.time()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.time()'</span></code> or <codeclass="python hljs"><spanclass="hljs-string">'<DTa>.timetz()'</span></code>.</strong></li>
</ul>
<div><h3id="timezone">Timezone</h3><pre><codeclass="python language-python apache hljs"><tzinfo> = timezone.utc <spanclass="hljs-comment"># London without daylight saving time.</span>
<div><h3id="timezone">Timezone</h3><pre><codeclass="python language-python apache hljs"><tzinfo> = timezone.utc <spanclass="hljs-comment"># London without daylight saving time (DST).</span>
<tzinfo> = timezone(<timedelta>) <spanclass="hljs-comment"># Timezone with fixed offset from UTC.</span>
<tzinfo> = tzlocal() <spanclass="hljs-comment"># Local timezone. Also gettz().</span>
<tzinfo> = gettz(<spanclass="hljs-string">'<Continent>/<City>'</span>) <spanclass="hljs-comment"># 'Continent/City_Name' timezone or None.</span>
<DTa> = <DT>.astimezone([<tzinfo>]) <spanclass="hljs-comment"># Converts DT to the passed or local timezone.</span>
<DTa> = <DT>.astimezone([<tzinfo>]) <spanclass="hljs-comment"># Converts DT to the passed or local fixed zone.</span>
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) <spanclass="hljs-comment"># Changes object's timezone without conversion.</span>
</code></pre></div>
<ul>
<li><strong>Timezones returned by gettz(), tzlocal(), and implicit local timezone of naive objects have offsets that vary through time due to DST and historical changes of the zone's base offset.</strong></li>
<li><strong>Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.</strong></li>
</ul>
<div><h3id="encode">Encode</h3><pre><codeclass="python language-python apache hljs"><D/T/DT> = D/T/DT.fromisoformat(<spanclass="hljs-string">'<iso>'</span>) <spanclass="hljs-comment"># Object from ISO string. Raises ValueError.</span>
<str> = <D/T/DT>.strftime(<spanclass="hljs-string">'<format>'</span>) <spanclass="hljs-comment"># Custom string representation of the object.</span>
<int> = <D/DT>.toordinal() <spanclass="hljs-comment"># Days since Gregorian NYE 1, ignoring time and tz.</span>
<float> = <DTn>.timestamp() <spanclass="hljs-comment"># Seconds since the Epoch, from DTn in local tz.</span>
<float> = <DTa>.timestamp() <spanclass="hljs-comment"># Seconds since the Epoch, from aware datetime.</span>
<spanclass="hljs-meta">>>></span>dt.strftime(<spanclass="hljs-string">"%dth of %B '%y (%a), %I:%M%p %Z"</span>)
<spanclass="hljs-string">"14th of August '25 (Thu), 11:39PM UTC+02:00"</span>
</code></pre></div>
<ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'%z'</span></code> accepts <codeclass="python hljs"><spanclass="hljs-string">'±HH[:]MM'</span></code> and returns <codeclass="python hljs"><spanclass="hljs-string">'±HHMM'</span></code> or empty string if datetime is naive.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'%Z'</span></code> accepts <codeclass="python hljs"><spanclass="hljs-string">'UTC/GMT'</span></code> and local timezone's code and returns timezone's name, <codeclass="python hljs"><spanclass="hljs-string">'UTC[±HH:MM]'</span></code> if timezone is nameless, or an empty string if datetime is naive.</strong></li>
<li><strong>For abbreviated weekday and month use <codeclass="python hljs"><spanclass="hljs-string">'%a'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'%b'</span></code>.</strong></li>
</ul>
<div><h3id="arithmetics">Arithmetics</h3><pre><codeclass="python language-python apache hljs"><D/DT> = <D/DT> ± <TD><spanclass="hljs-comment"># Returned datetime can fall into missing hour.</span>
<TD> = <D/DTn> - <D/DTn><spanclass="hljs-comment"># Returns the difference. Ignores time jumps.</span>
<TD> = <DTa> - <DTa><spanclass="hljs-comment"># Ignores time jumps if they share tzinfo object.</span>