0% found this document useful (0 votes)
34 views

Creating A Python Script For Date Assignments Involves Using The Datetime Module

The document discusses how to write a Python script to handle basic date assignments using the datetime module. It involves importing the datetime module, getting the current date, performing date calculations like adding days, checking if a date is a weekend, and formatting dates. It provides example functions and a sample script to demonstrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Creating A Python Script For Date Assignments Involves Using The Datetime Module

The document discusses how to write a Python script to handle basic date assignments using the datetime module. It involves importing the datetime module, getting the current date, performing date calculations like adding days, checking if a date is a weekend, and formatting dates. It provides example functions and a sample script to demonstrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating a Python script for date assignments involves using the datetime module, which

provides classes for manipulating dates and times. Below, I'll show you how to write a script
that can handle basic date assignments, such as calculating due dates, checking if a date is a
weekend, and formatting dates.

Step-by-Step Guide

1. Import the datetime module: This module provides the necessary functions for date
manipulation.
2. Get the current date: Use the datetime.date.today() function.
3. Perform date calculations: Add or subtract days to find due dates.
4. Check for weekends: Determine if a given date falls on a weekend.
5. Format dates: Convert dates to a string in various formats.

Example Script
python
import datetime

# Function to add days to a given date


def add_days(start_date, days):
return start_date + datetime.timedelta(days=days)

# Function to check if a date is a weekend


def is_weekend(date):
return date.weekday() >= 5 # 5 = Saturday, 6 = Sunday

# Function to format a date


def format_date(date, format_string="%Y-%m-%d"):
return date.strftime(format_string)

# Main script
def main():
# Get the current date
today = datetime.date.today()
print(f"Today's date: {format_date(today)}")

# Calculate a due date (e.g., 10 days from today)


due_date = add_days(today, 10)
print(f"Due date (10 days from today): {format_date(due_date)}")

# Check if the due date is a weekend


if is_weekend(due_date):
print("The due date falls on a weekend.")
else:
print("The due date is a weekday.")

# Custom format for the due date


formatted_due_date = format_date(due_date, "%A, %B %d, %Y")
print(f"Formatted due date: {formatted_due_date}")

if __name__ == "__main__":
main()

Explanation
1. Importing the datetime module:

python
 import datetime

 Defining functions:

 add_days(start_date, days): Adds a specified number of days to a given date.


 is_weekend(date): Checks if a given date is a Saturday or Sunday.
 format_date(date, format_string="%Y-%m-%d"): Formats a date into a specified
string format.

 Main script:

 Get today's date:

python
 today = datetime.date.today()
 Calculate the due date:
python
 due_date = add_days(today, 10)
 Check if the due date is a weekend:
python
 if is_weekend(due_date):
print("The due date falls on a weekend.")
else:
print("The due date is a weekday.")
 Format the due date:
python

3.
o formatted_due_date = format_date(due_date, "%A, %B %d, %Y")
o

Running the Script

Save the script in a file, for example, date_assignments.py, and run it using a Python
interpreter:

sh
python date_assignments.py

This will output today's date, the due date 10 days from today, whether the due date is a
weekend, and the formatted due date. You can customize the number of days, date formats,
and other parameters as needed.

You might also like