Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions python_tips_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,157 @@
"for i in generator([1,2,3,4]):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Datetime\n",
"\n",
"The datetime module helps programmers manage and manipulate dates and times in Python with ease. This module is builtin, and further explanation and examples are to be found in the [documentation](https://docs.python.org/3/library/datetime.html)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2018, 9, 27, 11, 2, 34, 623437)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import datetime\n",
"\n",
"# Display the (llocal!) current time and date\n",
"datetime.now()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, we now have a datetime object, which has the following attributes: year, month, day, hour, minute, seconds, micro-seconds.\n",
"We can also extract just the data we need from the datetime object, as follows."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Today is the 27 day of month 9, and the year is 2018!\n"
]
}
],
"source": [
"now_datetime_object = datetime.now()\n",
"\n",
"# Extract only certain data\n",
"\n",
"now_year = now_datetime_object.year\n",
"now_month = now_datetime_object.month\n",
"now_day = now_datetime_object.day\n",
"\n",
"print('Today is the {} day of month {}, and the year is {}!'.format(now_day, now_month, now_year))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. of month 9 looks quite bad.\n",
"That's where the method strftime() comes into play."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The current month is September.\n"
]
}
],
"source": [
"# strftime() formats datetime objects into readable strings\n",
"\n",
"print('The current month is {}.'.format(now_datetime_object.strftime(\"%B\")))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strftime() can generally be used to format datetime objects."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Today's date is 27-09-2018, and be careful; now it is not a datetimeobject!\n",
" it is actually <class 'str'>!\n"
]
}
],
"source": [
"pretty_format = now_datetime_object.strftime('%d-%m-%Y')\n",
"print(\"Today's date is {}, and be careful; now it is not a datetimeobject!\\n \\\n",
" it is actually {}!\".format(pretty_format, type(pretty_format)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get a date X days\\months\\etc from now, we use timedelta()."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I started studying in university back in 21-10-2015, and I'll finish in 17-10-2018\n"
]
}
],
"source": [
"from datetime import timedelta\n",
"\n",
"# School lasts about three years, so we calculate the time difference between starting university time and three years \n",
"# (or 3 * 52 weeks) from this time\n",
"\n",
"start_uni_time = datetime(2015, 10, 21)\n",
"end_uni_time = start_uni_time + timedelta(weeks=52 * 3)\n",
"print(\"I started studying in university back in {}, and I'll finish in {}\".format(\n",
" start_uni_time.strftime('%d-%m-%Y'), end_uni_time.strftime('%d-%m-%Y')))"
]
}
],
"metadata": {
Expand Down