|
| 1 | +Date |
| 2 | +==== |
| 3 | + |
| 4 | +The ``Date`` class is a drop-in replacement for the native ``date`` |
| 5 | +class (it is inherited from it). |
| 6 | + |
| 7 | +It shares a lot of methods and attributes with the ``Pendulum`` class. |
| 8 | + |
| 9 | + |
| 10 | +Instantiation |
| 11 | +------------- |
| 12 | + |
| 13 | +There are several different methods available to create a new ``Date`` object. |
| 14 | +First there is a constructor. It accepts the same parameters as the standard class. |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + from pendulum import Date |
| 19 | +
|
| 20 | + dt = Date(2016, 11, 26) |
| 21 | + isinstance(dt, date) |
| 22 | + True |
| 23 | +
|
| 24 | +You can also create known instances by using the corresponding methods: |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + today = Date.today() |
| 29 | + print(today) |
| 30 | + '2016-11-26' |
| 31 | +
|
| 32 | + tomorrow = Date.tomorrow() |
| 33 | + print(tomorrow) |
| 34 | + '2016-11-27' |
| 35 | +
|
| 36 | + yesterday = Date.yesterday() |
| 37 | + print(yesterday) |
| 38 | + '2016-11-25' |
| 39 | +
|
| 40 | +Next there is the ``create()`` helper. |
| 41 | + |
| 42 | +You can provide as many or as few arguments as you want and the helper will provide default values for all others. |
| 43 | +Generally default values are the current date. |
| 44 | + |
| 45 | +.. code-block:: python |
| 46 | +
|
| 47 | + Date.create() |
| 48 | + '2016-11-26' |
| 49 | +
|
| 50 | + Date.create(2015) |
| 51 | + '2015-11-26' |
| 52 | +
|
| 53 | + Date.create(2015, 10, 11) |
| 54 | + '2015-10-11' |
| 55 | +
|
| 56 | +Finally, if you find yourself inheriting a ``date`` instance, |
| 57 | +you can create a ``Date`` instance via the ``instance()`` method. |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + dt = date(2016, 11, 26) |
| 62 | + d = Date.instance(dt) |
| 63 | + print(d) |
| 64 | + '2016-11-26' |
| 65 | +
|
| 66 | +
|
| 67 | +Localization |
| 68 | +------------ |
| 69 | + |
| 70 | +Localization occurs when using the ``format()`` method which accepts a ``locale`` keyword. |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + from pendulum import Date |
| 75 | +
|
| 76 | + dt = Date(1975, 5, 21) |
| 77 | +
|
| 78 | + dt.format('%A %d %B %Y', locale='de') |
| 79 | + 'Mittwoch 21 Mai 1975' |
| 80 | +
|
| 81 | + dt.format('%A %d %B %Y') |
| 82 | + 'Wednesday 21 May 1975' |
| 83 | +
|
| 84 | +``diff_for_humans()`` is also localized, you can set the locale globally |
| 85 | +by using the ``pendulum.set_locale()``. |
| 86 | + |
| 87 | +.. code-block:: python |
| 88 | +
|
| 89 | + import pendulum |
| 90 | + from pendulum import Date |
| 91 | +
|
| 92 | + pendulum.set_locale('de') |
| 93 | + print(Date.today().add(years=1).diff_for_humans()) |
| 94 | + 'in 1 Jahr' |
| 95 | +
|
| 96 | + pendulum.set_locale('en') |
| 97 | +
|
| 98 | +However, you might not want to set the locale globally. |
| 99 | +The ``diff_for_humans()`` method accept a ``locale`` keyword argument to use a locale for a specific call. |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + pendulum.set_locale('de') |
| 104 | + print(Date.today().add(years=1).diff_for_humans(locale='fr')) |
| 105 | + 'dans 1 an' |
| 106 | +
|
| 107 | +
|
| 108 | +Attributes and Properties |
| 109 | +------------------------- |
| 110 | + |
| 111 | +Pendulum gives access to more attributes and properties than the default ``date`` class. |
| 112 | + |
| 113 | +.. code-block:: python |
| 114 | +
|
| 115 | + from pendulum import Date |
| 116 | +
|
| 117 | + dt = Date(2012, 9, 5) |
| 118 | +
|
| 119 | + # These properties specifically return integers |
| 120 | + dt.year |
| 121 | + 2012 |
| 122 | + dt.month |
| 123 | + 9 |
| 124 | + dt.day |
| 125 | + 5 |
| 126 | + dt.day_of_week |
| 127 | + 3 |
| 128 | + dt.day_of_year |
| 129 | + 248 |
| 130 | + dt.week_of_month |
| 131 | + 1 |
| 132 | + dt.week_of_year |
| 133 | + 36 |
| 134 | + dt.days_in_month |
| 135 | + 30 |
| 136 | + dt.quarter |
| 137 | + 3 |
| 138 | +
|
| 139 | +
|
| 140 | +Comparison |
| 141 | +---------- |
| 142 | + |
| 143 | +You can refer to the corresponding `section <#comparison>`_ of the documentation. |
| 144 | + |
| 145 | + |
| 146 | +Addition and Subtraction |
| 147 | +------------------------ |
| 148 | + |
| 149 | +You can refer to the corresponding `section <#addition-and-subtraction>`_ of the documentation. |
| 150 | + |
| 151 | + |
| 152 | +Difference |
| 153 | +---------- |
| 154 | + |
| 155 | +You can refer to the corresponding `section <#difference>`_ of the documentation. |
| 156 | + |
| 157 | + |
| 158 | +Modifiers |
| 159 | +--------- |
| 160 | + |
| 161 | +You can refer to the corresponding `section <#modifiers>`_ of the documentation. |
0 commit comments