0% found this document useful (0 votes)
8 views2 pages

Date Handling Exercises

The document outlines a series of Python exercises focused on date handling, including creating and displaying dates, parsing date strings, performing date arithmetic, determining weekdays, and working with time zones. Each exercise consists of specific tasks such as printing the current date, calculating future dates, and checking for leap years. Additionally, it introduces the use of the pytz library for managing time zones effectively.

Uploaded by

Surabhi Rastogi
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)
8 views2 pages

Date Handling Exercises

The document outlines a series of Python exercises focused on date handling, including creating and displaying dates, parsing date strings, performing date arithmetic, determining weekdays, and working with time zones. Each exercise consists of specific tasks such as printing the current date, calculating future dates, and checking for leap years. Additionally, it introduces the use of the pytz library for managing time zones effectively.

Uploaded by

Surabhi Rastogi
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

Date Handling Exercises using Python

Exercise 1: Basic Date & Time Creation and Display


1. Current Date and Time: Get and print the current date and time.
2. Current Date Only: Get and print only the current date.
3. Specific Date: Create a date object for your birthday (e.g., May 15, 1990) and
print it.
4. Specific Datetime: Create a datetime object for January 1, 2025, at 10:30 AM
and print it.
5. Formatted Output: Print the current date and time in the format: "YYYY-MM-DD
HH:MM:SS" (e.g., "2023-10-27 14:35:01").
6. Custom Format: Print your birthday (from step 3) in the format: "Day, Month DD,
YYYY" (e.g., "Wednesday, May 15, 1990").
Exercise 2: Parsing Dates from Strings
1. Basic Parsing: Parse the string "2023-03-15" into a date object.
2. Datetime Parsing: Parse the string "2024-07-22 14:30:00" into a datetime
object.
3. Mixed Format: Parse the string "Dec 25, 2023 10:00 PM" into a datetime object.
4. ISO Format: Parse the ISO 8601 formatted string "2023-11-01T09:00:00" into a
datetime object.
Exercise 3: Date Arithmetic (Timedelta)
1. Future Date: Calculate and print the date 7 days from today.
2. Past Date: Calculate and print the date 3 weeks ago from today.
3. Future Time: Calculate and print the time 5 hours and 30 minutes from now.
4. Date Difference: Calculate the number of days between "2023-01-01" and
"2023-12-31".
5. Age Calculation: Calculate your age in days (from your birthday to today).

Exercise 4: Weekdays and Calendars


1. Day of the Week: For a given date (e.g., October 27, 2023), print the day of the
week (e.g., "Friday").
2. Is it a Weekend? Write a function that takes a date object and returns True if it's
a weekend (Saturday or Sunday), False otherwise. Test it with a weekday and a
weekend date.
3. Leap Year Check: Write a function that takes a year (integer) and returns True if
it's a leap year, False otherwise. Test it with 2024, 2023, 2000, and 1900.
(Hint: A year is a leap year if it is divisible by 4, unless it is divisible by 100 but not
by 400.)
Exercise 5: Time Zones (Advanced)
Work with time zones using the pytz library (or zoneinfo in Python 3.9+). For simplicity,
we'll use pytz as it's widely compatible. You might need to install it (pip install pytz).
1. Current UTC Time: Get and print the current UTC time.
2. Local Time in Specific Timezone: Get the current time in "America/New_York"
timezone.
3. Convert Timezone: Convert the current UTC time to "Asia/Kolkata" timezone.
4. Naive to Aware: Create a naive datetime object (without timezone info) and
make it timezone-aware for "Europe/London".

You might also like