Python Calendar Functions

First things first, let’s import the `datetime` module. This is where all the magic happens!

# Import the datetime module
import datetime

# Create a variable called "today" and assign it the current date using the datetime module's "date" function
today = datetime.date.today()

# Print the current date in the format of "Month Day, Year"
print(today.strftime("%B %d, %Y"))

# Create a variable called "current_time" and assign it the current time using the datetime module's "time" function
current_time = datetime.time()

# Print the current time in the format of "Hour:Minute:Second"
print(current_time.strftime("%H:%M:%S"))

# Create a variable called "current_datetime" and assign it the current date and time using the datetime module's "now" function
current_datetime = datetime.datetime.now()

# Print the current date and time in the format of "Month Day, Year Hour:Minute:Second"
print(current_datetime.strftime("%B %d, %Y %H:%M:%S"))

Now that we have our trusty wizarding wand (aka the `datetime` module), let’s see what kind of spells it can cast for us! ️️

1. Current Time: If you want to know what time it is right now, simply call the `datetime.now()` function and voila! You’ll have a datetime object that contains all the information about the current date and time. ️

# Import the datetime module
import datetime

# Call the datetime.now() function to get the current date and time
current_time = datetime.now()

# Print the current date and time
print(current_time)

# Output: datetime.datetime(2021, 7, 3, 9, 45, 36, 85200)

# The datetime.now() function returns a datetime object that contains information about the current date and time. 
# This object can be used to perform various operations related to date and time.

2. Formatting Dates and Times: Sometimes you might want to format your dates and times in a specific way for display purposes. The `datetime.strftime()` function allows us to do just that! Simply pass the datetime object as an argument, followed by a formatting string (which is basically a template with placeholders).

# Import the datetime module
import datetime

# Create a datetime object with the current date and time
current_time = datetime.now()

# Use the strftime() function to format the datetime object
# The formatting string '%Y-%m-%d %H:%M:%S' specifies the desired format
# %Y represents the year in four digits, %m represents the month in two digits, %d represents the day in two digits, 
# %H represents the hour in 24-hour format, %M represents the minute in two digits, and %S represents the second in two digits
formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S')

# Print the formatted time
print(formatted_time)

# Output: 2021-07-03 09:45:36

In this example, we’re using the `%Y`, `%m`, and `%d` placeholders to format the year (four digits), month (two digits with leading zero if necessary), and day respectively. We’re also using `%H`, `%M`, and `%S` for hours, minutes, and seconds respectively.

3. Calculating Dates: If you want to calculate dates based on a given date or time, the `datetime.timedelta()` function is your new best friend!

# Import the datetime module
import datetime

# Get the current date and time
current_time = datetime.datetime.now()

# Use the timedelta function to add 30 days to the current date and time
future_date = current_time + datetime.timedelta(days=30)

# Print the future date and time
print(future_date)

# Output: datetime.datetime(2021, 8, 1, 9, 45, 36, 85200)

# The datetime module allows us to work with dates and times in Python
# The datetime.now() function returns the current date and time
# The timedelta function allows us to add or subtract time from a given date or time
# In this case, we are adding 30 days to the current date and time
# The result is stored in the future_date variable
# The print function displays the future date and time in the specified format

In this example, we’re adding a `timedelta()` object with 30 days to the current time using the `+` operator. This will give us the date and time that is exactly one month from now!

4. Checking Dates: If you want to check if a given date or time falls within a certain range, use the `datetime.date()` function to extract just the date portion of your datetime object. Then compare it with other dates using comparison operators like `<`, `>`, and `==`.

# Import the necessary modules
import datetime

# Get the current time
current_time = datetime.datetime.now()

# Get the future date by adding 30 days to the current time
future_date = current_time + datetime.timedelta(days=30)

# Get the current date
today = datetime.datetime.now().date()

# Check if today's date is before the future date
if today < future_date.date():
    # If true, print a message
    print('Today is before the future date!')
    
# Output: Today is before the future date!

# Explanation:
# The datetime module is imported to work with dates and times.
# The current_time variable is assigned the current date and time using the now() function.
# The future_date variable is assigned the current date and time plus 30 days using the timedelta() function.
# The today variable is assigned the current date using the now() function and extracting only the date portion using the date() function.
# The if statement checks if today's date is before the future date by comparing the two dates using the comparison operator < (less than).
# If the condition is true, the message "Today is before the future date!" is printed.

In this example, we’re extracting just the date portion of our `future_date` object using the `.date()` method. We then compare it with the current date (which was also extracted from the datetime object) to see if today falls within 30 days before or on the future date!

And that’s all for now, my dear wizards! I hope you enjoyed this magical journey through Python calendar functions! Remember, always use your powers for good and never let them fall into the wrong hands.

SICORPS