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

Calculate Next Date in Python

Good
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Calculate Next Date in Python

Good
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

def is_leap_year(year):

"""
Returns True if the specified year is a leap year, otherwise False.
"""
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
return False

def next_date(year, month, day):


"""
Returns the next date as a tuple (year, month, day).
"""
# Days in each month accounting for leap years
days_in_month = [31, 29 if is_leap_year(year) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Move to the next day


day += 1

# Check if we need to move to the next month


if day > days_in_month[month - 1]:
day = 1
month += 1

# Check if we need to move to the next year


if month > 12:
month = 1
year += 1

return year, month, day

def main():
while True:
# Input the current date
year = int(input("Enter year (1812-2024): "))
month = int(input("Enter month (1-12): "))
day = int(input("Enter day (1-31): "))

# Validate the input year


if year < 1812 or year > 2024:
print("Year must be between 1812 and 2024. Please try again.")
continue

# Validate the input month


if month < 1 or month > 12:
print("Month must be between 1 and 12. Please try again.")
continue

# Days in each month accounting for leap years


days_in_month = [31, 29 if is_leap_year(year) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31]

# Validate the input day


if day < 1 or day > days_in_month[month - 1]:
print(f"Day must be between 1 and {days_in_month[month - 1]} for month {month}.
Please try again.")
continue

# Get the next date


next_year, next_month, next_day = next_date(year, month, day)

# Print the next date


print(f"The next date is: {next_year}-{next_month:02d}-{next_day:02d}")
break

if __name__ == "__main__":
main()

You might also like