blog image

Python Problems on Datetime question

February 16, 20243 min read

Problem 1: Display the Current Date and Time

Problem Description: Write a Python program that uses the datetime library to display the current date and time in the format YYYY-MM-DD HH:MM:SS.

Sample Input: No input required.

Expected Output:2023-04-05 14:30:45 (Note: Output will vary based on the current date and time.)

Problem 2: Calculate Age

Problem Description: Create a program that calculates a person's age based on their birthdate entered by the user. The birthdate should be input in the format YYYY-MM-DD.

Sample Input:2000-01-01

Expected Output:23 years old (Note: Output will vary based on the current date and the input date.)

Problem 3: Days Until New Year

Problem Description: Write a Python script that calculates the number of days from the current date until the next New Year's Day.

Sample Input: No input required.

Expected Output:88 days until New Year (Note: Output will vary based on the current date.)

Problem 4: Weekday of Birth

Problem Description: Ask the user to enter their birthdate in the format YYYY-MM-DD, and then display the day of the week they were born on.

Sample Input:2000-01-01

Expected Output:Saturday

Problem 5: Adding Days to a Date

Problem Description: Write a Python program that asks the user to enter a date in the format YYYY-MM-DD and a number of days to add to this date. The program should then display the new date after adding the specified days.

Sample Input:

  • Date: 2023-01-01

  • Days to add: 30

Expected Output:2023-01-31

Problem 6: Difference Between Two Dates

Problem Description: Create a program that calculates the difference in days between two dates entered by the user. Both dates should be input in the format YYYY-MM-DD.

Sample Input:

  • First date: 2023-01-01

  • Second date: 2023-01-31

Expected Output:30 days

Problem 7: Display the Month Name

Problem Description: Ask the user to enter a date in the format YYYY-MM-DD. The program should then display the name of the month for that date.

Sample Input:2023-03-15

Expected Output:March

Problem 8: First Day of the Year

Problem Description: Write a program that asks the user to enter a year, then prints the date of the first Monday of that year.

Sample Input:2023

Expected Output:2023-01-02

Problem 9: End of the Month Date

Problem Description: Write a Python program that asks the user to enter a year and a month (as numbers), then prints the last day of that month.

Sample Input:

  • Year: 2023

  • Month: 2

Expected Output:2023-02-28

Problem 10: Number of Weekdays in a Month

Problem Description: Create a program that calculates the number of Mondays (or any specified weekday) in a given month and year entered by the user.

Sample Input:

  • Year: 2023

  • Month: 3

  • Weekday: Monday

Problem 11: Age in Days

Problem Description: Write a Python script that asks the user for their birthdate and calculates their age in days as of today.

Sample Input:2000-01-01

Expected Output:Approximately 8505 days old (Note: Output will vary based on the current date.)

Problem 12: Adding Hours to Time

Problem Description: Create a program that asks the user for a time in the format HH:MM and a number of hours to add. The program should then print the new time.

Sample Input:

  • Time: 14:30

  • Hours to add: 5

Expected Output:19:30

blog author image

sunil s

Quant Developer & Mentor

Back to Blog