|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### 1. Datetime Module\n", |
| 8 | + " - Manipulation date objects\n", |
| 9 | + " \n", |
| 10 | + "```\n", |
| 11 | + " \n", |
| 12 | + " datetime.datetime : date object with date and time\n", |
| 13 | + " \n", |
| 14 | + " datetime.timedelta : difference between dates or datetimes\n", |
| 15 | + " \n", |
| 16 | + " datetime.timezone : timezone adjustments\n", |
| 17 | + "\n", |
| 18 | + " ```" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 3, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [ |
| 26 | + { |
| 27 | + "name": "stdout", |
| 28 | + "output_type": "stream", |
| 29 | + "text": [ |
| 30 | + "2017\n", |
| 31 | + "11\n", |
| 32 | + "1\n", |
| 33 | + "0\n" |
| 34 | + ] |
| 35 | + } |
| 36 | + ], |
| 37 | + "source": [ |
| 38 | + "import datetime\n", |
| 39 | + "\n", |
| 40 | + "# current date and time object\n", |
| 41 | + "now = datetime.datetime.now()\n", |
| 42 | + "\n", |
| 43 | + "print(now.year)\n", |
| 44 | + "print(now.hour)\n", |
| 45 | + "print(now.minute)\n", |
| 46 | + "# Return the day of the week as an integer, where Monday is 0 and Sunday is 6.\n", |
| 47 | + "print(now.weekday())" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "### 2. Date object to string\n", |
| 55 | + "\n", |
| 56 | + " - http://strftime.org/" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 6, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "data": { |
| 66 | + "text/plain": [ |
| 67 | + "'2018-11-10 17:53:59'" |
| 68 | + ] |
| 69 | + }, |
| 70 | + "execution_count": 6, |
| 71 | + "metadata": {}, |
| 72 | + "output_type": "execute_result" |
| 73 | + } |
| 74 | + ], |
| 75 | + "source": [ |
| 76 | + "dt_obj = datetime.datetime(2018, 11, 10, 17, 53, 59)\n", |
| 77 | + "date_str = dt_obj.strftime(\"%Y-%m-%d %H:%M:%S\")\n", |
| 78 | + "date_str" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "### 3. String to Datetime object" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 8, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "data": { |
| 95 | + "text/plain": [ |
| 96 | + "datetime.datetime(2008, 11, 10, 17, 53, 59)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + "execution_count": 8, |
| 100 | + "metadata": {}, |
| 101 | + "output_type": "execute_result" |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "date_str = \"2008-11-10 17:53:59\"\n", |
| 106 | + "dt_obj = datetime.datetime.strptime(date_str, \"%Y-%m-%d %H:%M:%S\")\n", |
| 107 | + "dt_obj" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "### 4. Timestamp to Datetime object" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 10, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "data": { |
| 124 | + "text/plain": [ |
| 125 | + "datetime.datetime(2008, 11, 12, 21, 59, 27, 595983)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + "execution_count": 10, |
| 129 | + "metadata": {}, |
| 130 | + "output_type": "execute_result" |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "# timestamp to datetime object in UTC\n", |
| 135 | + "timestamp = 1226527167.595983\n", |
| 136 | + "dt_obj = datetime.datetime.utcfromtimestamp(timestamp)\n", |
| 137 | + "dt_obj" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "### 5. Difference between two Dates" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 11, |
| 150 | + "metadata": { |
| 151 | + "collapsed": true |
| 152 | + }, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "end_date = datetime.datetime.now()\n", |
| 156 | + "start_date = end_date - datetime.timedelta(days=8)\n", |
| 157 | + "difference_in_days = abs((end_date - start_date).days)" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": 12, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [ |
| 165 | + { |
| 166 | + "data": { |
| 167 | + "text/plain": [ |
| 168 | + "8" |
| 169 | + ] |
| 170 | + }, |
| 171 | + "execution_count": 12, |
| 172 | + "metadata": {}, |
| 173 | + "output_type": "execute_result" |
| 174 | + } |
| 175 | + ], |
| 176 | + "source": [ |
| 177 | + "difference_in_days" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "### TRY YOURSELF\n", |
| 185 | + "\n", |
| 186 | + " 1) WAP to convert a string to datetime.\n", |
| 187 | + " \n", |
| 188 | + " Example input : Jan 1 2014 2:43PM \n", |
| 189 | + " Output : 2014-07-01 14:43:00" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": null, |
| 195 | + "metadata": { |
| 196 | + "collapsed": true |
| 197 | + }, |
| 198 | + "outputs": [], |
| 199 | + "source": [] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "Python 3", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.5.2" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 2 |
| 223 | +} |
0 commit comments