Built with Tutify.io

    AIStudyLabs

    Working with dates and times (datetime)

    Working with dates and times (datetime)

    Introduction

    In this lesson, we delve into the Python datetime module, a powerful feature of the Python Standard Library that allows you to work with dates and times effectively. This lesson is tailored to enhance your ability to handle real-world time-based tasks, including formatting, parsing, arithmetic operations, and timezone adjustments.

    Differentiating datetime.date, datetime.time, and datetime.datetime

    The datetime module comprises three primary classes:

    Formatting and Parsing Dates and Times

    Formatting and parsing dates and times are critical for input and output. The strftime method formats date/time objects into readable strings, whereas strptime parses strings into datetime objects.

    Arithmetic with Dates and Times using timedelta

    The timedelta class enables arithmetic operations on dates and times, useful for calculations like adding days to a schedule:

    from datetime import datetime, timedelta
    
    initial_date = datetime(2023, 10, 1)
    delta = timedelta(days=5)
    new_date = initial_date + delta
    print(new_date)  # Output: 2023-10-06
    

    Handling Time Zones and Conversions

    Python’s datetime supports time zones via the timezone class, allowing you to convert between different time zones making datetime objects time zone aware:

    from datetime import datetime, timezone, timedelta
    
    naive_utc = datetime(2023, 10, 1, 15, 30)  # Naive datetime
    utc_datetime = naive_utc.replace(tzinfo=timezone.utc)
    print(utc_datetime)  # Output: 2023-10-01 15:30:00+00:00
    
    local_datetime = utc_datetime.astimezone(timezone(timedelta(hours=5, minutes=30)))
    print(local_datetime)  # Output: 2023-10-01 21:00:00+05:30
    

    Retrieving and Converting Between Naive and Aware datetime

    Naive datetimes lack timezone information; aware datetimes include it. Getting the current timestamp:

    from datetime import datetime, timezone
    
    current_time = datetime.now()  # Naive datetime
    utc_time = datetime.now(timezone.utc)  # Aware datetime
    print(current_time)  # Output: e.g. 2023-10-01 15:30:00
    print(utc_time)  # Output: e.g. 2023-10-01 15:30:00+00:00
    

    Summary / Key Takeaways

    Python Programming – Complete Course with Practical Examples — Python Standard Library for Real-World Tasks

    Getting Started with Python and the Course

    Python Syntax, Indentation, and Core Concepts

    Variables, Data Types, and Type Conversion

    Operators, Expressions, and Control Flow

    Loops and Iteration Patterns

    Working with Strings in Depth

    Collections: Lists, Tuples, Sets, and Dictionaries

    Functions and Program Structuring

    Functional Programming Concepts in Python

    Modules, Packages, and Code Organization

    Error Handling and Debugging Fundamentals

    File Handling and Data Formats

    Object-Oriented Programming in Python

    Advanced Python: Iterators, Generators, and Decorators

    Python Standard Library for Real-World Tasks

    Python for Automation and Scripting

    Testing and Quality Assurance in Python

    Tutorial Not Found