10000 Add files via upload · qma09/08_Python_Date_Time_Module@d7a99d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7a99d6

Browse files
authored
Add files via upload
1 parent 531b182 commit d7a99d6

8 files changed

+3512
-0
lines changed

001_Python_datetime_Module.ipynb

Lines changed: 1094 additions & 0 deletions
Large diffs are not rendered by default.

002_Python_strftime().ipynb

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

0 commit comments

Comments
 (0)
0