Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09f0dde

Browse files
committed
#8913: add examples and docs for date/time/datetime.__format__. Patch by Heikki Partanen.
1 parent cf658c2 commit 09f0dde

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

Doc/library/datetime.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ Instance methods:
597597
section :ref:`strftime-strptime-behavior`.
598598

599599

600+
.. method:: date.__format__(format)
601+
602+
Same as :meth:`.date.strftime`. This makes it possible to specify format
603+
string for a :class:`.date` object when using :meth:`str.format`.
604+
See section :ref:`strftime-strptime-behavior`.
605+
606+
600607
Example of counting days to an event::
601608

602609
>>> import time
@@ -647,6 +654,8 @@ Example of working with :class:`date`:
647654
'11/03/02'
648655
>>> d.strftime("%A %d. %B %Y")
649656
'Monday 11. March 2002'
657+
>>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")
658+
'The day is 11, the month is March.'
650659

651660

652661
.. _datetime-datetime:
@@ -1154,6 +1163,13 @@ Instance methods:
11541163
string. See section :ref:`strftime-strptime-behavior`.
11551164

11561165

1166+
.. method:: datetime.__format__(format)
1167+
1168+
Same as :meth:`.datetime.strftime`. This makes it possible to specify format
1169+
string for a :class:`.datetime` object when using :meth:`str.format`.
1170+
See section :ref:`strftime-strptime-behavior`.
1171+
1172+
11571173
Examples of working with datetime objects:
11581174

11591175
.. doctest::
@@ -1198,6 +1214,8 @@ Examples of working with datetime objects:
11981214
>>> # Formatting datetime
11991215
>>> dt.strftime("%A, %d. %B %Y %I:%M%p")
12001216
'Tuesday, 21. November 2006 04:30PM'
1217+
>>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
1218+
'The day is 21, the month is November, the time is 04:30PM.'
12011219

12021220
Using datetime with tzinfo:
12031221

@@ -1385,6 +1403,13 @@ Instance methods:
13851403
See section :ref:`strftime-strptime-behavior`.
13861404

13871405

1406+
.. method:: time.__format__(format)
1407+
1408+
Same as :meth:`.time.strftime`. This makes it possible to specify format string
1409+
for a :class:`.time` object when using :meth:`str.format`.
1410+
See section :ref:`strftime-strptime-behavior`.
1411+
1412+
13881413
.. method:: time.utcoffset()
13891414

13901415
If :attr:`tzinfo` is ``None``, returns ``None``, else returns
@@ -1431,6 +1456,8 @@ Example:
14311456
'Europe/Prague'
14321457
>>> t.strftime("%H:%M:%S %Z")
14331458
'12:10:30 Europe/Prague'
1459+
>>> 'The {} is {:%H:%M}.'.format("time", t)
1460+
'The time is 12:10.'
14341461

14351462

14361463
.. _datetime-tzinfo:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ Peter Parente
907907
Alexandre Parenteau
908908
Dan Parisien
909909
William Park
910+
Heikki Partanen
910911
Harri Pasanen
911912
Gaël Pasgrimaud
912913
Randy Pausch

0 commit comments

Comments
 (0)