|
| 1 | +# Python `datetime` module |
| 2 | + |
| 3 | +**Video link:** |
| 4 | + |
| 5 | +In this video, we learned about the `datetime` module to manipulate date and time in Python. |
| 6 | + |
| 7 | +**Programs in the Video** |
| 8 | + |
| 9 | +- [Get Current Date](#get-current-date) |
| 10 | +- [The datetime.date Class](#the-datetimedate-class) |
| 11 | +- [The datetime.time Class](#the-datetimetime-class) |
| 12 | +- [The datetime.datetime Class](#the-datetimedatetime-class) |
| 13 | +- [Getting current date and time](#getting-current-date-and-time) |
| 14 | +- [The datetime.timedelta Class](#the-datetimetimedelta-class) |
| 15 | +- [Python `strftime()` method](#python-strftime-method) |
| 16 | +- [Python `strptime()` method](#python-strptime-method) |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Get Current Date |
| 21 | +We can find the current local date using the `today()` method of the `date` class of the `datetime` module: |
| 22 | + |
| 23 | +```python |
| 24 | +import datetime as dt |
| 25 | + |
| 26 | +current_date = dt.date.today() |
| 27 | +print(current_date) |
| 28 | +``` |
| 29 | + |
| 30 | +**Output** |
| 31 | +``` |
| 32 | +2021-02-10 |
| 33 | +``` |
| 34 | + |
| 35 | +Similar to the `date` class, the `datetime` module has many other useful classes to work with date and time: |
| 36 | + |
| 37 | +* `date` class - to work with date |
| 38 | +* `time` class - to work with time |
| 39 | +* `datetime` class - combination of `date` and `time` classes |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## The datetime.date Class |
| 44 | + |
| 45 | +The `date` class of the `datetime` module is used to create `date` objects that can store year, month and day. |
| 46 | + |
| 47 | +```python |
| 48 | +import datetime as dt |
| 49 | + |
| 50 | +date1 = dt.date(2021, 1, 21) |
| 51 | +print(date1) |
| 52 | + |
| 53 | +print("Year:", date1.year) |
| 54 | +print("Month:", date1.month) |
| 55 | +print("Day:", date1.day) |
| 56 | +``` |
| 57 | + |
| 58 | +**Output** |
| 59 | + |
| 60 | +``` |
| 61 | +2021-01-21 |
| 62 | +Year: 2021 |
| 63 | +Month: 1 |
| 64 | +Day: 21 |
| 65 | +``` |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## The datetime.time Class |
| 70 | + |
| 71 | +The `time` class of the `datetime` module is used to create `time` objects that can store time of day like hours, minutes, seconds, and microseconds. |
| 72 | + |
| 73 | +The time class takes in all optional integer arguments. By default all of them are `0`. |
| 74 | +- The first argument is hour from `0` to `24` |
| 75 | +- The second one is minutes from `0` to `60` |
| 76 | +- The third is seconds from `0` to `60` |
| 77 | +- The fourth is microsecond from `0` to `999999` |
| 78 | + |
| 79 | +```python |
| 80 | +import datetime as dt |
| 81 | + |
| 82 | +time1 = dt.time(10, 47, 20, 234566) |
| 83 | +print(time1) |
| 84 | + |
| 85 | +print("Hour:", time1.hour) |
| 86 | +print("Minute:", time1.minute) |
| 87 | +print("Second:", time1.second) |
| 88 | +print("Microsecond:", time1.microsecond) |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +**Output** |
| 93 | + |
| 94 | +``` |
| 95 | +10:47:20.234566 |
| 96 | +Hour: 10 |
| 97 | +Minute: 47 |
| 98 | +Second: 20 |
| 99 | +Microsecond: 234566 |
| 100 | +``` |
| 101 | + |
| 102 | +>**Note:** If you want more control and functionalities on time, use the `time` module. |
| 103 | +
|
| 104 | +--- |
| 105 | + |
| 106 | +## The datetime.datetime Class |
| 107 | + |
| 108 | +The `datetime` class of the `datetime` module is used to create objects that contain all the information from a `date` object as well as a `time` object. |
| 109 | + |
| 110 | +```python |
| 111 | +import datetime as dt |
| 112 | +datetime_obj = dt.datetime(2021, 11, 28, 23, 55, 59) |
| 113 | + |
| 114 | +print(datetime_obj) |
| 115 | + |
| 116 | +print(datetime_obj.date()) |
| 117 | +print(datetime_obj.time()) |
| 118 | +``` |
| 119 | + |
| 120 | +**Output** |
| 121 | +``` |
| 122 | +2021-11-28 23:55:59 |
| 123 | +2021-11-28 |
| 124 | +23:55:59 |
| 125 | +``` |
| 126 | + |
| 127 | +Like with `date` and `time` objects, we can also access individual attributes like `year`, `month` and `hour` as previously discussed. |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## Getting current date and time |
| 132 | + |
| 133 | +To get the current local date and time at once, we can use the `now()` method of the `datetime` object. |
| 134 | + |
| 135 | +```python |
| 136 | +import datetime as dt |
| 137 | + |
| 138 | +current_time = dt.datetime.now() |
| 139 | + |
| 140 | +print(current_time) |
| 141 | +``` |
| 142 | + |
| 143 | +**Output** |
| 144 | +``` |
| 145 | +2021-01-21 05:56:45.817533 |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## The datetime.timedelta Class |
| 151 | + |
| 152 | +A timedelta object represents the difference between two dates or times. |
| 153 | + |
| 154 | +Let's find out the time difference between now and the next new year. |
| 155 | + |
| 156 | +```python |
| 157 | +import datetime as dt |
| 158 | + |
| 159 | +current_time = dt.datetime.now() |
| 160 | +next_new_year = dt.datetime(2022, 1, 1) |
| 161 | + |
| 162 | +time_remaining = next_new_year - current_time |
| 163 | + |
| 164 | +print(time_remaining) |
| 165 | +print(type(time_remaining)) |
| 166 | +``` |
| 167 | + |
| 168 | +**Output** |
| 169 | +``` |
| 170 | +324 days, 13:31:29.981402 |
| 171 | +<class 'datetime.timedelta'> |
| 172 | +``` |
| 173 | + |
| 174 | +This `timedelta` object can also be added or subtracted from `datetime` objects to get new `datetime` objects. |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +## Python `strftime()` method |
| 179 | + |
| 180 | +The `strftime()` method returns a string representing date and time for the datetime object. |
| 181 | + |
| 182 | +There are many formats to write the date and time depending on your location. |
| 183 | + |
| 184 | +If you are in the US, you probably use the `mm-dd-yyyy` format while if you're in the UK you will generally use the `dd-mm-yyyy` format. |
| 185 | + |
| 186 | +The `strftime()` method allows us to display the date and time in a custom specific format. |
| 187 | + |
| 188 | +```python |
| 189 | +import datetime as dt |
| 190 | + |
| 191 | +current_datetime = dt.datetime.now() |
| 192 | +print(current_datetime) |
| 193 | + |
| 194 | +string_date = current_datetime.strftime("%A, %B %d, %Y") |
| 195 | +print(string_date) |
| 196 | +``` |
| 197 | + |
| 198 | +**Output** |
| 199 | +``` |
| 200 | +2021-01-21 06:20:19.627086 |
| 201 | +Thursday, January 21, 2021 |
| 202 | +``` |
| 203 | + |
| 204 | +Here, |
| 205 | +- `%A` represents the weekday name i.e. `Thursday` |
| 206 | +- `%B` represents the month's full name |
| 207 | +- `%d` represents the day of the month |
| 208 | +- `%Y` represents the year |
| 209 | + |
| 210 | +There are many other format codes: |
| 211 | + |
| 212 | +| Directive | Meaning | Example | |
| 213 | +|-----------|-------------------------------------------|---------------------| |
| 214 | +| `%a` | Abbreviated weekday name | Sun, Mon, ... | |
| 215 | +| `%A` | Full weekday name | Sunday, Monday, ... | |
| 216 | +| `%w` | Weekday as a decimal number | 0, 1, ..., 6 | |
| 217 | +| `%d` | Day of the month as a zero-padded decimal | 01, 02, ..., 31 | |
| 218 | +| `%b` | Abbreviated month name | Jan, Feb, ..., Dec | |
| 219 | +| `%p` | Locale’s AM or PM | AM, PM | |
| 220 | + |
| 221 | +--- |
| 222 | + |
| 223 | +## Python `strptime()` method |
| 224 | + |
| 225 | +The `strptime()` method converts strings to datetime objects. |
| 226 | + |
| 227 | +```python |
| 228 | +import datetime as dt |
| 229 | + |
| 230 | +date_string = "21 June, 2021" |
| 231 | + |
| 232 | +date_object = dt.datetime.strptime(date_string, "%d %B, %Y") |
| 233 | +print("date_object:", date_object) |
| 234 | +``` |
| 235 | + |
| 236 | +**Output** |
| 237 | +``` |
| 238 | +date_object: 2018-06-21 00:00:00 |
| 239 | +``` |
0 commit comments