In this Python program, we are going to see how we can calculate Year, Week and Days from given total days. We all know that 1 year = 365 (ignoring leap year) and 1 week = 7 days on this basis we can determine the year, week and days from given total no of days.
Logic
If we divide the total number of days by 365 it will return a Year. But to calculate week and days logic is a little different. First, we need the remainder value of (Total days % 365) from this value we can determine total no of weeks and days. To calculate total no of weeks simply divide the remainder value by 7 and to calculate total no of days mode (%) it by 7.
Program
# Take input from user days = int(input("Enter total number of days : ")) # Calculate year, week and days year = int(days / 365) temp = int(days % 365) week = int(temp / 7) days = int(temp % 7) # Print the result print("\nYears :", year) print("Week :", week) print("Days :", days)
Output
Enter total number of days : 1212
Years : 3
Weeks : 16
Days : 5
Explanation
Calculating year – Divide the total no. of given days with 365.
year = int(days / 365)
Calculating week – Mod the total given days with 365 and divide it with 7 (no of weeks).
temp = int(days % 365) week = temp / 7
Calculating days – Mod the total user given days with 365 and again mod the result with 7.
temp = int(days % 365) days = temp % 7