1
+ {
2
+ "metadata" : {
3
+ "name" : " " ,
4
+ "signature" : " sha256:54f7c7737775a39294a7ad5f519a6d41130263335b942927e415405b0924d91f"
5
+ },
6
+ "nbformat" : 3 ,
7
+ "nbformat_minor" : 0 ,
8
+ "worksheets" : [
9
+ {
10
+ "cells" : [
11
+ {
12
+ "cell_type" : " markdown" ,
13
+ "metadata" : {},
14
+ "source" : [
15
+ " # Date And Time Basics\n " ,
16
+ " \n " ,
17
+ " This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)."
18
+ ]
19
+ },
20
+ {
21
+ "cell_type" : " markdown" ,
22
+ "metadata" : {},
23
+ "source" : [
24
+ " ### Import modules"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type" : " code" ,
29
+ "collapsed" : false ,
30
+ "input" : [
31
+ " from datetime import datetime\n " ,
32
+ " from datetime import timedelta"
33
+ ],
34
+ "language" : " python" ,
35
+ "metadata" : {},
36
+ "outputs" : [],
37
+ "prompt_number" : 24
38
+ },
39
+ {
40
+ "cell_type" : " markdown" ,
41
+ "metadata" : {},
42
+ "source" : [
43
+ " ### Create a variable with the current time"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type" : " code" ,
48
+ "collapsed" : false ,
49
+ "input" : [
50
+ " now = datetime.now()\n " ,
51
+ " now"
52
+ ],
53
+ "language" : " python" ,
54
+ "metadata" : {},
55
+ "outputs" : [
56
+ {
57
+ "metadata" : {},
58
+ "output_type" : " pyout" ,
59
+ "prompt_number" : 25 ,
60
+ "text" : [
61
+ " datetime.datetime(2014, 5, 11, 20, 5, 11, 688051)"
62
+ ]
63
+ }
64
+ ],
65
+ "prompt_number" : 25
66
+ },
67
+ {
68
+ "cell_type" : " markdown" ,
69
+ "metadata" : {},
70
+ "source" : [
71
+ " ### The current year"
72
+ ]
73
+ },
74
+ {
75
+ "cell_type" : " code" ,
76
+ "collapsed" : false ,
77
+ "input" : [
78
+ " now.year"
79
+ ],
80
+ "language" : " python" ,
81
+ "metadata" : {},
82
+ "outputs" : [
83
+ {
84
+ "metadata" : {},
85
+ "output_type" : " pyout" ,
86
+ "prompt_number" : 26 ,
87
+ "text" : [
88
+ " 2014"
89
+ ]
90
+ }
91
+ ],
92
+ "prompt_number" : 26
93
+ },
94
+ {
95
+ "cell_type" : " markdown" ,
96
+ "metadata" : {},
97
+ "source" : [
98
+ " ### The current month"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type" : " code" ,
103
+ "collapsed" : false ,
104
+ "input" : [
105
+ " now.month"
106
+ ],
107
+ "language" : " python" ,
108
+ "metadata" : {},
109
+ "outputs" : [
110
+ {
111
+ "metadata" : {},
112
+ "output_type" : " pyout" ,
113
+ "prompt_number" : 27 ,
114
+ "text" : [
115
+ " 5"
116
+ ]
117
+ }
118
+ ],
119
+ "prompt_number" : 27
120
+ },
121
+ {
122
+ "cell_type" : " markdown" ,
123
+ "metadata" : {},
124
+ "source" : [
125
+ " ### The current day"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type" : " code" ,
130
+ "collapsed" : false ,
131
+ "input" : [
132
+ " now.day"
133
+ ],
134
+ "language" : " python" ,
135
+ "metadata" : {},
136
+ "outputs" : [
137
+ {
138
+ "metadata" : {},
139
+ "output_type" : " pyout" ,
140
+ "prompt_number" : 28 ,
141
+ "text" : [
142
+ " 11"
143
+ ]
144
+ }
145
+ ],
146
+ "prompt_number" : 28
147
+ },
148
+ {
149
+ "cell_type" : " markdown" ,
150
+ "metadata" : {},
151
+ "source" : [
152
+ " ### The current hour"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type" : " code" ,
157
+ "collapsed" : false ,
158
+ "input" : [
159
+ " now.hour"
160
+ ],
161
+ "language" : " python" ,
162
+ "metadata" : {},
163
+ "outputs" : [
164
+ {
165
+ "metadata" : {},
166
+ "output_type" : " pyout" ,
167
+ "prompt_number" : 29 ,
168
+ "text" : [
169
+ " 20"
170
+ ]
171
+ }
172
+ ],
173
+ "prompt_number" : 29
174
+ },
175
+ {
176
+ "cell_type" : " markdown" ,
177
+ "metadata" : {},
178
+ "source" : [
179
+ " ### The current minute"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type" : " code" ,
184
+ "collapsed" : false ,
185
+ "input" : [
186
+ " now.minute"
187
+ ],
188
+ "language" : " python" ,
189
+ "metadata" : {},
190
+ "outputs" : [
191
+ {
192
+ "metadata" : {},
193
+ "output_type" : " pyout" ,
194
+ "prompt_number" : 30 ,
195
+ "text" : [
196
+ " 5"
197
+ ]
198
+ }
199
+ ],
200
+ "prompt_number" : 30
201
+ },
202
+ {
203
+ "cell_type" : " markdown" ,
204
+ "metadata" : {},
205
+ "source" : [
206
+ " ### The difference between two dates"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type" : " code" ,
211
+ "collapsed" : false ,
212
+ "input" : [
213
+ " delta = datetime(2011, 1, 7) - datetime(2011, 1, 6)\n " ,
214
+ " delta"
215
+ ],
216
+ "language" : " python" ,
217
+ "metadata" : {},
218
+ "outputs" : [
219
+ {
220
+ "metadata" : {},
221
+ "output_type" : " pyout" ,
222
+ "prompt_number" : 37 ,
223
+ "text" : [
224
+ " datetime.timedelta(1)"
225
+ ]
226
+ }
227
+ ],
228
+ "prompt_number" : 37
229
+ },
230
+ {
231
+ "cell_type" : " markdown" ,
232
+ "metadata" : {},
233
+ "source" : [
234
+ " ### The difference days"
235
+ ]
236
+ },
237
+ {
238
+ "cell_type" : " code" ,
239
+ "collapsed" : false ,
240
+ "input" : [
241
+ " delta.days"
242
+ ],
243
+ "language" : " python" ,
244
+ "metadata" : {},
245
+ "outputs" : [
246
+ {
247
+ "metadata" : {},
248
+ "output_type" : " pyout" ,
249
+ "prompt_number" : 38 ,
250
+ "text" : [
251
+ " 1"
252
+ ]
253
+ }
254
+ ],
255
+ "prompt_number" : 38
256
+ },
257
+ {
258
+ "cell_type" : " markdown" ,
259
+ "metadata" : {},
260
+ "source" : [
261
+ " ### The difference seconds"
262
+ ]
263
+ },
264
+ {
265
+ "cell_type" : " code" ,
266
+ "collapsed" : false ,
267
+ "input" : [
268
+ " delta.seconds"
269
+ ],
270
+ "language" : " python" ,
271
+ "metadata" : {},
272
+ "outputs" : [
273
+ {
274
+ "metadata" : {},
275
+ "output_type" : " pyout" ,
276
+ "prompt_number" : 39 ,
277
+ "text" : [
278
+ " 0"
279
+ ]
280
+ }
281
+ ],
282
+ "prompt_number" : 39
283
+ },
284
+ {
285
+ "cell_type" : " markdown" ,
286
+ "metadata" : {},
287
+ "source" : [
288
+ " ### Create a time"
289
+ ]
290
+ },
291
+ {
292
+ "cell_type" : " code" ,
293
+ "collapsed" : false ,
294
+ "input" : [
295
+ " start = datetime(2011, 1, 7)"
296
+ ],
297
+ "language" : " python" ,
298
+ "metadata" : {},
299
+ "outputs" : [],
300
+ "prompt_number" : 35
301
+ },
302
+ {
303
+ "cell_type" : " markdown" ,
304
+ "metadata" : {},
305
+ "source" : [
306
+ " ### Add twelve days to the time"
307
+ ]
308
+ },
309
+ {
310
+ "cell_type" : " code" ,
311
+ "collapsed" : false ,
312
+ "input" : [
313
+ " start + timedelta(12)"
314
+ ],
315
+ "language" : " python" ,
316
+ "metadata" : {},
317
+ "outputs" : [
318
+ {
319
+ "metadata" : {},
320
+ "output_type" : " pyout" ,
321
+ "prompt_number" : 36 ,
322
+ "text" : [
323
+ " datetime.datetime(2011, 1, 19, 0, 0)"
324
+ ]
325
+ }
326
+ ],
327
+ "prompt_number" : 36
328
+ }
329
+ ],
330
+ "metadata" : {}
331
+ }
332
+ ]
333
+ }
0 commit comments