|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in **Python Date Time Module** lecture series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/08_Python_Date_Time_Module)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python `strftime()`\n", |
| 17 | + "\n", |
| 18 | + "In this class, you will learn to convert date, time and datetime objects to its equivalent string (with the help of examples).\n", |
| 19 | + "\n", |
| 20 | + "The **[strftime()](https://strftime.org/)** method returns a string representing date and time using **[date (@ Example 3), time (@ Example 7) or datetime (@ Example 9)](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/001_Python_datetime_Module.ipynb)** object." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "### Example 1: datetime to string using `strftime()`" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "The program below converts a **`datetime`** object containing current date and time to different string formats." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 1, |
| 40 | + "metadata": { |
| 41 | + "ExecuteTime": { |
| 42 | + "end_time": "2021-10-18T12:31:58.196637Z", |
| 43 | + "start_time": "2021-10-18T12:31:58.163438Z" |
| 44 | + } |
| 45 | + }, |
| 46 | + "outputs": [ |
| 47 | + { |
| 48 | + "name": "stdout", |
| 49 | + "output_type": "stream", |
| 50 | + "text": [ |
| 51 | + "2021-10-18 18:01:58.168321\n", |
| 52 | + "year: 2021\n", |
| 53 | + "month: 10\n", |
| 54 | + "day: 18\n", |
| 55 | + "time: 18:01:58\n", |
| 56 | + "date and time: 10/18/2021, 18:01:58\n", |
| 57 | + "date and time: 18/10/2021, 18:01:58\n" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "# Example 1: datetime to string using strftime()\n", |
| 63 | + "\n", |
| 64 | + "from datetime import datetime\n", |
| 65 | + "\n", |
| 66 | + "now = datetime.now() # current date and time\n", |
| 67 | + "print(now)\n", |
| 68 | + "\n", |
| 69 | + "year = now.strftime(\"%Y\")\n", |
| 70 | + "print(\"year:\", year)\n", |
| 71 | + "\n", |
| 72 | + "month = now.strftime(\"%m\")\n", |
| 73 | + "print(\"month:\", month)\n", |
| 74 | + "\n", |
| 75 | + "day = now.strftime(\"%d\")\n", |
| 76 | + "print(\"day:\", day)\n", |
| 77 | + "\n", |
| 78 | + "time = now.strftime(\"%H:%M:%S\")\n", |
| 79 | + "print(\"time:\", time)\n", |
| 80 | + "\n", |
| 81 | + "date_time1 = now.strftime(\"%m/%d/%Y, %H:%M:%S\")\n", |
| 82 | + "print(\"date and time:\",date_time1)\n", |
| 83 | + "\n", |
| 84 | + "date_time2 = now.strftime(\"%d/%m/%Y, %H:%M:%S\")\n", |
| 85 | + "print(\"date and time:\",date_time2)\n", |
| 86 | + "\n", |
| 87 | + "# When you run the program, the output will be something like below:" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + ">**Note:** Here, **`year`**, **`day`**, **`time`** and **`date_time`** are strings, whereas **`now`** is a **`datetime`** object." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "## How `strftime()` works?\n", |
| 102 | + "\n", |
| 103 | + "In the above program, **`%Y`**, **`%m`**, **`%d`** etc. are format codes. The **`strftime()`** method takes one or more format codes as an argument and returns a formatted string based on it." |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "1. We imported **`datetime`** class from the **`datetime`** module. It's because the object of **`datetime`** class can access **`strftime()`** method.\n", |
| 111 | + "\n", |
| 112 | + "<div>\n", |
| 113 | + "<img src=\"img/idt.png\" width=\"400\"/>\n", |
| 114 | + "</div>\n", |
| 115 | + "\n", |
| 116 | + "2. The **`datetime`** object containing current date and time is stored in **`now`** variable.\n", |
| 117 | + "\n", |
| 118 | + "<div>\n", |
| 119 | + "<img src=\"img/cdt.png\" width=\"400\"/>\n", |
| 120 | + "</div>\n", |
| 121 | + "\n", |
| 122 | + "3. The **`strftime()`** method can be used to create formatted strings.\n", |
| 123 | + "\n", |
| 124 | + "<div>\n", |
| 125 | + "<img src=\"img/strff1.png\" width=\"400\"/>\n", |
| 126 | + "</div>\n", |
| 127 | + "\n", |
| 128 | + "4. The string you pass to the **`strftime()`** method may contain more than one format codes.\n", |
| 129 | + "\n", |
| 130 | + "<div>\n", |
| 131 | + "<img src=\"img/strff2.png\" width=\"400\"/>\n", |
| 132 | + "</div>" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### Example 2: Creating string from a timestamp" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 2, |
| 145 | + "metadata": { |
| 146 | + "ExecuteTime": { |
| 147 | + "end_time": "2021-10-18T12:31:58.489119Z", |
| 148 | + "start_time": "2021-10-18T12:31:58.199569Z" |
| 149 | + } |
| 150 | + }, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "name": "stdout", |
| 154 | + "output_type": "stream", |
| 155 | + "text": [ |
| 156 | + "Date time object: 2018-06-12 15:25:22\n", |
| 157 | + "Output 2: 06/12/2018, 15:25:22\n", |
| 158 | + "Output 3: 12 Jun, 2018\n", |
| 159 | + "Output 4: 12 June, 2018\n", |
| 160 | + "Output 5: 03PM\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "# Example 2: Creating string from a timestamp\n", |
| 166 | + "\n", |
| 167 | + "from datetime import datetime\n", |
| 168 | + "\n", |
| 169 | + "timestamp = 1528797322\n", |
| 170 | + "date_time = datetime.fromtimestamp(timestamp)\n", |
| 171 | + "\n", |
| 172 | + "print(\"Date time object:\", date_time)\n", |
| 173 | + "\n", |
| 174 | + "d = date_time.strftime(\"%m/%d/%Y, %H:%M:%S\")\n", |
| 175 | + "print(\"Output 2:\", d)\t\n", |
| 176 | + "\n", |
| 177 | + "d = date_time.strftime(\"%d %b, %Y\")\n", |
| 178 | + "print(\"Output 3:\", d)\n", |
| 179 | + "\n", |
| 180 | + "d = date_time.strftime(\"%d %B, %Y\")\n", |
| 181 | + "print(\"Output 4:\", d)\n", |
| 182 | + "\n", |
| 183 | + "d = date_time.strftime(\"%I%p\")\n", |
| 184 | + "print(\"Output 5:\", d)\n", |
| 185 | + "\n", |
| 186 | + "# When you run the program, the output will be something like below:" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "metadata": { |
| 192 | + "cell_style": "center" |
| 193 | + }, |
| 194 | + "source": [ |
| 195 | + "## Format Code List\n", |
| 196 | + "\n", |
| 197 | + "The table below shows all the codes that you can pass to the **`strftime()`** method.\n", |
| 198 | + "\n", |
| 199 | + "| Directive | Meaning | Example |\n", |
| 200 | + "|:----: |:---- |:---- |\n", |
| 201 | + "| **`%a`** | **Abbreviated weekday name.** | **Sun, Mon, ...** | \n", |
| 202 | + "| **`%A`** | **Full weekday name.** | **Sunday, Monday, ...** | \n", |
| 203 | + "| **`%w`** | **Weekday as a decimal number.** | **0, 1, ..., 6** | \n", |
| 204 | + "| **`%d`** | **Day of the month as a zero-padded decimal.** | **01, 02, ..., 31** | \n", |
| 205 | + "| **`%-d`** | **Day of the month as a decimal number.** | **1, 2, ..., 30** | \n", |
| 206 | + "| **`%b`** | **Abbreviated month name.** | **Jan, Feb, ..., Dec** | \n", |
| 207 | + "| **`%B`** | **Full month name.** | **January, February, ...** | \n", |
| 208 | + "| **`%m`** | **Month as a zero-padded decimal number.** | **01, 02, ..., 12** | \n", |
| 209 | + "| **`%-m`** | **Month as a decimal number.** | **1, 2, ..., 12** | \n", |
| 210 | + "| **`%y`** | **Year without century as a zero-padded decimal number.** | **00, 01, ..., 99** | \n", |
| 211 | + "| **`%-y`** | **Year without century as a decimal number.** | **0, 1, ..., 99** | \n", |
| 212 | + "| **`%Y`** | **Year with century as a decimal number.** | **2013, 2019 etc.** | \n", |
| 213 | + "| **`%H`** | **Hour (24-hour clock) as a zero-padded decimal number.** | **00, 01, ..., 23** | \n", |
| 214 | + "| **`%-H`** | **Hour (24-hour clock) as a decimal number.** | **0, 1, ..., 23** | \n", |
| 215 | + "| **`%I`** | **Hour (12-hour clock) as a zero-padded decimal number.** | **01, 02, ..., 12** | \n", |
| 216 | + "| **`%-I`** | **Hour (12-hour clock) as a decimal number.** | **1, 2, ... 12** | \n", |
| 217 | + "| **`%p`** | **Locale’s AM or PM.** | **AM, PM** | \n", |
| 218 | + "| **`%M`** | **Minute as a zero-padded decimal number.** | **00, 01, ..., 59** | \n", |
| 219 | + "| **`%-M`** | **Minute as a decimal number.** | **0, 1, ..., 59** | \n", |
| 220 | + "| **`%S`** | **Second as a zero-padded decimal number.** | **00, 01, ..., 59** | \n", |
| 221 | + "| **`%-S`** | **Second as a decimal number.** | **0, 1, ..., 59** | \n", |
| 222 | + "| **`%f`** | **Microsecond as a decimal number, zero-padded on the left.** | **000000 - 999999** | \n", |
| 223 | + "| **`%z`** | **UTC offset in the form +HHMM or -HHMM.** | | \n", |
| 224 | + "| **`%Z`** | **Time zone name.** | | \n", |
| 225 | + "| **`%j`** | **Day of the year as a zero-padded decimal number.** | **001, 002, ..., 366** | \n", |
| 226 | + "| **`%-j`** | **Day of the year as a decimal number.** | **1, 2, ..., 366** | \n", |
| 227 | + "| **`%U`** | **Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.** | **00, 01, ..., 53** | \n", |
| 228 | + "| **`%U`** | **Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.** | **00, 01, ..., 53** | \n", |
| 229 | + "| **`%c`** | **Locale’s appropriate date and time representation.** | **Mon Sep 30 07:06:05 2013** | \n", |
| 230 | + "| **`%x`** | **Locale’s appropriate date representation.** | **09/30/13** | \n", |
| 231 | + "| **`%X`** | **Locale’s appropriate time representation.** | **07:06:05** | \n", |
| 232 | + "| **`%%`** | **A literal '%' character.** | **%** | " |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "metadata": {}, |
| 238 | + "source": [ |
| 239 | + "### Example 3: Locale's appropriate date and time" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": 3, |
| 245 | + "metadata": { |
| 246 | + "ExecuteTime": { |
| 247 | + "end_time": "2021-10-18T12:31:58.628277Z", |
| 248 | + "start_time": "2021-10-18T12:31:58.500841Z" |
| 249 | + } |
| 250 | + }, |
| 251 | + "outputs": [ |
| 252 | + { |
| 253 | + "name": "stdout", |
| 254 | + "output_type": "stream", |
| 255 | + "text": [ |
| 256 | + "Output 1: Tue Jun 12 15:25:22 2018\n", |
| 257 | + "Output 2: 06/12/18\n", |
| 258 | + "Output 3: 15:25:22\n" |
| 259 | + ] |
| 260 | + } |
| 261 | + ], |
| 262 | + "source": [ |
| 263 | + "# Example 3: Locale's appropriate date and time\n", |
| 264 | + "\n", |
| 265 | + "from datetime import datetime\n", |
| 266 | + "\n", |
| 267 | + "timestamp = 1528797322\n", |
| 268 | + "date_time = datetime.fromtimestamp(timestamp)\n", |
| 269 | + "\n", |
| 270 | + "d = date_time.strftime(\"%c\")\n", |
| 271 | + "print(\"Output 1:\", d)\t\n", |
| 272 | + "\n", |
| 273 | + "d = date_time.strftime(\"%x\")\n", |
| 274 | + "print(\"Output 2:\", d)\n", |
| 275 | + "\n", |
| 276 | + "d = date_time.strftime(\"%X\")\n", |
| 277 | + "print(\"Output 3:\", d)\n", |
| 278 | + "\n", |
| 279 | + "# When you run the program, the output will be something like below:" |
| 280 | + ] |
| 281 | + }, |
| 282 | + { |
| 283 | + "cell_type": "markdown", |
| 284 | + "metadata": {}, |
| 285 | + "source": [ |
| 286 | + ">**Note:** Format codes **`%c`**, **`%x`** and **`%X`** are used for locale's appropriate date and time representation." |
| 287 | + ] |
| 288 | + }, |
| 289 | + { |
| 290 | + "cell_type": "markdown", |
| 291 | + "metadata": {}, |
| 292 | + "source": [ |
| 293 | + "We also recommend you to check **[Python strptime()](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/003_Python_strptime%28%29.ipynb)**. The **`strptime()`** method creates a **`datetime`** object from a string." |
| 294 | + ] |
| 295 | + }, |
| 296 | + { |
| 297 | + "cell_type": "code", |
| 298 | + "execution_count": null, |
| 299 | + "metadata": {}, |
| 300 | + "outputs": [], |
| 301 | + "source": [] |
| 302 | + } |
| 303 | + ], |
| 304 | + "metadata": { |
| 305 | + "hide_input": false, |
| 306 | + "kernelspec": { |
| 307 | + "display_name": "Python 3", |
| 308 | + "language": "python", |
| 309 | + "name": "python3" |
| 310 | + }, |
| 311 | + "language_info": { |
| 312 | + "codemirror_mode": { |
| 313 | + "name": "ipython", |
| 314 | + "version": 3 |
| 315 | + }, |
| 316 | + "file_extension": ".py", |
| 317 | + "mimetype": "text/x-python", |
| 318 | + "name": "python", |
| 319 | + "nbconvert_exporter": "python", |
| 320 | + "pygments_lexer": "ipython3", |
| 321 | + "version": "3.8.8" |
| 322 | + }, |
| 323 | + "toc": { |
| 324 | + "base_numbering": 1, |
| 325 | + "nav_menu": {}, |
| 326 | + "number_sections": true, |
| 327 | + "sideBar": true, |
| 328 | + "skip_h1_title": false, |
| 329 | + "title_cell": "Table of Contents", |
| 330 | + "title_sidebar": "Contents", |
| 331 | + "toc_cell": false, |
| 332 | + "toc_position": {}, |
| 333 | + "toc_section_display": true, |
| 334 | + "toc_window_display": false |
| 335 | + }, |
| 336 | + "varInspector": { |
| 337 | + "cols": { |
| 338 | + "lenName": 16, |
| 339 | + "lenType": 16, |
| 340 | + "lenVar": 40 |
| 341 | + }, |
| 342 | + "kernels_config": { |
| 343 | + "python": { |
| 344 | + "delete_cmd_postfix": "", |
| 345 | + "delete_cmd_prefix": "del ", |
| 346 | + "library": "var_list.py", |
| 347 | + "varRefreshCmd": "print(var_dic_list())" |
| 348 | + }, |
| 349 | + "r": { |
| 350 | + "delete_cmd_postfix": ") ", |
| 351 | + "delete_cmd_prefix": "rm(", |
| 352 | + "library": "var_list.r", |
| 353 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 354 | + } |
| 355 | + }, |
| 356 | + "types_to_exclude": [ |
| 357 | + "module", |
| 358 | + "function", |
| 359 | + "builtin_function_or_method", |
| 360 | + "instance", |
| 361 | + "_Feature" |
| 362 | + ], |
| 363 | + "window_display": false |
| 364 | + } |
| 365 | + }, |
| 366 | + "nbformat": 4, |
| 367 | + "nbformat_minor": 2 |
| 368 | +} |
0 commit comments