Python Date and Time

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will step into the world of a 19th-century Victorian era, where a mysterious detective is on a mission to unravel a perplexing case involving cryptic time-related clues. The ambiance of this era, shrouded in mystery and elegance, sets the stage for our endeavor. The detective, known for their astute observational skills and deductive reasoning, must leverage Python's date and time capabilities to decipher the hidden messages in the case they are investigating.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") subgraph Lab Skills python/date_time -.-> lab-271543{{"`Python Date and Time`"}} end

Setting the Stage with Time Data

In this step, we will start by creating a Python script to retrieve and display the current date and time. The detective will use this script to verify the timeline of events and establish critical reference points for their investigation.

In ~/project/display_time.py:

## File: ~/project/display_time.py

import datetime

current_time = datetime.datetime.now()
print("Current date and time:", current_time)

Run the script:

python display_time.py

The information below should be displayed on your terminal:

Current date and time: 2024-01-17 18:54:37.798326

Time Manipulation for Clue Analysis

In this step, we will enhance the detective's script to manipulate time data. They will utilize Python's datetime module to subtract and add time intervals, simulating the process of decoding time-related clues crucial to the case.

In ~/project/time_manipulation.py:

## File: ~/project/time_manipulation.py

import datetime

current_time = datetime.datetime.now()
print("Current date and time:", current_time)

## Subtracting 5 days from current time
new_time = current_time - datetime.timedelta(days=5)
print("Time 5 days ago:", new_time)

## Adding 3 hours to the current time
new_time = current_time + datetime.timedelta(hours=3)
print("Time 3 hours later:", new_time)

Run the script:

python time_manipulation.py

The information below should be displayed on your terminal:

Current date and time: 2024-01-17 18:55:36.077424
Time 5 days ago: 2024-01-12 18:55:36.077424
Time 3 hours later: 2024-01-17 21:55:36.077424

Converting Time Formats for Decoding Clues

In this step, the detective will extend their script to convert time data into different formats, essential for deciphering the varied time-related clues encountered during the investigation.

In ~/project/time_conversion.py:

## File: ~/project/time_conversion.py

import datetime

current_time = datetime.datetime.now()
print("Current date and time (ISO format):", current_time.isoformat())

## Converting time to a custom format
custom_format = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("Current date and time (custom format):", custom_format)

Run the script:

python time_conversion.py

The information below should be displayed on your terminal:

Current date and time (ISO format): 2024-01-17T18:57:11.001163
Current date and time (custom format): 2024-01-17 18:57:11

Summary

In this lab, we delved into Python's date and time capabilities to aid a mysterious detective in solving a perplexing case set in the midst of the Victorian era. By mastering the manipulation and conversion of time data, the detective honed their skills for unraveling cryptic time-related clues, ultimately leading to the resolution of the enigmatic case.

Other Python Tutorials you may like