{ "cells": [ { "cell_type": "markdown", "id": "30555cd1", "metadata": {}, "source": [ "# `datetime` Module\n", "\n", "One of the most indispensable tools at your disposal is the `datetime` module. This module is the Swiss Army knife for handling dates and times, offering a robust set of classes and functions to make working with temporal data a breeze. \n", "\n", "## Why is the `datetime` Module Important?\n", "\n", "The importance of the `datetime` module lies in its ability to empower developers to manipulate, format, and calculate dates and times effectively. It provides a standardized way of handling temporal data, making it crucial for a wide range of applications. Here are a few reasons why the `datetime` module is considered a cornerstone of Python programming:\n", "\n", "- **Date and Time Manipulation**: Python's `datetime` module allows you to create, modify, and manipulate dates and times effortlessly. Whether you're dealing with historical events, scheduling tasks, or calculating time intervals, this module has you covered.\n", "\n", "- **Cross-Platform Compatibility**: `datetime` offers a consistent interface to work with dates and times across different platforms and operating systems. This ensures that your code behaves predictably regardless of the environment it runs in.\n", "\n", "- **Localization and Timezones**: Handling timezones and localized date formatting can be challenging, but the `datetime` module simplifies these complexities. It helps you work with timezones and perform conversions with ease.\n", "\n", "- **Data Analysis**: For data scientists and analysts, `datetime` is an essential tool for analyzing time series data. It allows you to group, filter, and aggregate data based on timestamps, enabling insightful data-driven decisions.\n", "\n", "## Key Functions and Use Cases\n", "\n", "Now, let's take a closer look at some key functions within the `datetime` module and explore practical use cases with examples:\n", "\n", "1. `date()` - Creating Date Objects:\n", "\n", " The `datetime.date()` function is used to create date objects representing specific dates. For instance:" ] }, { "cell_type": "code", "execution_count": 3, "id": "bd721e57", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The British North America Act made Canada an independent country on 1871-07-01\n" ] } ], "source": [ "import datetime\n", "british_north_america_act = datetime.date(1871, 7, 1)\n", "\n", "print(f\"The British North America Act made Canada an independent country on {british_north_america_act}\")\n", "# TODO: add time(), datetime() and today()" ] }, { "cell_type": "markdown", "id": "e7c6ad3f-2ae8-475c-a946-d06966842e32", "metadata": {}, "source": [ "2. `time()`, `datetime()`, and `today()` - More Temporal Functions: \n", "\n", " - `time()` allows you to create time objects for precise time representation.\n", " - `datetime()` combines date and time information into a single object.\n", " - `today()` returns the current date.\n", "\n", "As we delve deeper into the `datetime` module, you'll discover its versatility in solving a wide array of real-world problems. From managing event schedules to calculating age, tracking deadlines, and conducting data analysis." ] }, { "cell_type": "markdown", "id": "dc3439ea", "metadata": {}, "source": [ "## Resources\n", "\n", "[![Socratica - DateTime Module](https://img.youtube.com/vi/RjMbCUpvIgw/maxresdefault.jpg)](https://youtu.be/RjMbCUpvIgw) " ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "formats": "ipynb,md:myst", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }