I need to find out the number of integer years, number of integer months, and number of integer days from the number of days, given as an input. However, my process is not working. I am getting the wrong answer.
Note: Assume, each year to be 365 days and each month to be 30 days. Also, please do not consider leap years.
user=int(input("Please enter the number of days: "))
def year(num):
year=int(num/365)
month=int((num%365)/30)
day=num%30
print(f"{year} years, {month} months and {day} days")
year(user)
I am getting 11 years, 10 months and 10 days as the output. However, the question prompt also gave me some sample outputs. In their sample output the output for 4330 was 11 years, 10 months and 15 days. Why are we getting different outputs? My code is flawless. I am getting what I expected to get. However, my expected output is not matching with the output given in my prompt.
I'm not sure why you're calculating the number of days using day=int((((num/365-int(num/365))*12)-month)*30), which I can't understand logically. A very simple, readable way would be:
def year(num):
year=num//365
month=(num%365)//30
day=num-(year*365)-(month*30)
print(f"{year} years, {month} months and {day} days")
year(4330) # prints 11 years, 10 months and 15 days
Edit:
day=num%30 wouldn't work, because it doesn't factor in the fact that an year has 365 days. Consider num = 730, which is 2 years and 0 days. Using your formula, you'll get day = 10, which is wrong.
I am trying to create an Excel formula that will display someone's age in units of days, weeks, months, or years, depending on how old they were at the time of a test. To clarify...
If younger than 7 days, report the age in days
If older than 7 days but younger than 8 weeks, report the age in weeks
If older than 8 weeks but younger than 24 months, report the age in months
If older than 24 months, report the age in years
I have tried several functions for this, all using the DATEDIF function. For each, I have returned the #NAME? error. Please see below
KEY:
E600 = Date of Test
F600 = Date of Birth
I am using 7 days = 1 week, 60 days = 2 months, 730 days = 24 months = 2 years, etc.
Note: I also want to display "d/o" (days old), "w/o" (weeks old), "m/o" (months old), or "y/o" (years old) after each value, depending on the units that age is being reported in.
\\\\\\\\\\\\
=IFS((E600-F600)<7,DATEDIF(F600,E600,”d”)&” d/o”,AND((E600-F600)>7,(E600-F600)<60),(DATEDIF(F600,E600,”d”)/7)&” w/o”,AND((E600-F600)>60,(E600-F600)<730),DATEDIF(F600,E600,”m”)&” m/o”,(E600-F600)>730,DATEDIF(F600,E600,”y”)&” y/o”)
\\\\\\\\\\\\\
=IFS(DATEDIF(F600,E600,”d”)<=6,DATEDIF(F600,E600,”d”)&” d/o”,AND(DATEDIF(F600,E600,”d”)<=59,DATEDIF(F600,E600,”d”)>=7),DATEDIF(F600,E600,”d”)/7&” w/o”,AND(DATEDIF(F600,E600,”d”)>=60,DATEDIF(F600,E600,”m”)<=24),DATEDIF(F600,E600,”m”)&” m/o”,DATEDIF(F600,E600,”m”)>25,DATEDIF(F600,E600,”y”)&” y/o”)
\\\\\\\\\\\\\
=DATEDIF(F600,E600,IFS((E600-F600)<7,”d”&” d/o”,AND((E600-F600)>7,(E600-F600)<60),”d”/7&” w/o”,AND((E600-F600)>60,(E600-F600)<730),”m”&” m/o”,(E600-F600)>730,”y”&” y/o”))
\\\\\\\\\\\\\\
Any help on this would be greatly appreciated!
=LOOKUP(E600-F600,{0,8,56,730},INT(DATEDIF(F600,E600,{"d","d","m","y"})/{1,7,1,1})&" "&{"d","w","m","y"}&"/o")
though your listed criteria state only "if younger than", "if older than", so it's not clear in which category someone who, for example, is precisely 7 days old should lie.
#Jos Woolley's example worked great. Another solution is posted below if anyone is troubleshooting their own formula in the future.
=IF(E600 < F600 + 7, DATEDIF(F600, E600, "d") & " d/o", IF(AND(E600 >= F600 + 7, E600 <= EDATE(F600, 2)), ROUNDDOWN(DATEDIF(F600, E600, "d") / 7, 0) & " w/o", IF(AND(E600 >= EDATE(F600, 2), E600 <= EDATE(F600, 24)), DATEDIF(F600, E600, "m") & " m/o", DATEDIF(F600, E600, "y") & " y/o")))
This is a bit of a complicated question for excel, so I'll try to explain as best I can.
I'm trying to track membership time for each member in a group in the member list which is an excel sheet. Currently I have 2 columns, one for years and one for months which are both calculating the proper time based on the Join date of the members. My issue comes when trying to calculate for the subsection of members who have 'broken' membership time, meaning they left the group and rejoined some time later. To correct for that I have a second sheet that tracks the previous time for those members (also in columns of years and months) and I'm trying to add it to the first sheet. The formulas I have are below:
-To calculate the number of years since the member joined, then add any previous years from the PrevTime sheet, matching on column B which is Name on my primary sheet:
=DATEDIF(F2,TODAY(),"y") + INDEX(PrevTime!A:C, MATCH(B2,PrevTime!A:A,0), 2)
-To calculate the number of months since the member joined, then add any previous months from the PrevTime sheet, matching on column B which is Name on my primary sheet.
=DATEDIF(F2,TODAY(),"ym") + INDEX(PrevTime!A:C, MATCH(B2,PrevTime!A:A,0), 3)
Now, everything works fine with both these formulas, the issue comes when the member in question has some combination of current membership time months, and previous membership months that exceed 12. Then I end up with one less year for their membership time and more than 12 months for their membership months. (i.e. 2 years, 15 Months instead of 3 years, 3 months).
Was just hoping someone here might have an idea or any insight into things I could try to sort this out so the times are displayed nicely without overflowing the months past 12.
Thanks in advance
I'm trying to make a calculator that calculates the amount of years, months and days from one date to another. The problem is that the calculation of days not always is accurate, or at least I don't think it is. I have downloaded four age calculators from GooglePlay store, and two of them sometimes give me a different result.
When I use the dates as shown in the picture some of the two of the calculators give me the answer 6 years, 8 months and 9 days.
The formulas I use is as follow:
C4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"Y")
E4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"ym")
F4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"md")
Is there a way to figure out if my calculation is correct?
Clearly on 22 April 2019 they would be exactly 6 years 0 months 0 days.Also 22 December 2019 they would be exactly 6 years 8 months 0 days. Starting from there:
Based on inspection the value of 10 days appears correct.
The excel user will export the data from an online website to excel (12 months data), so the data will be all the time different.
I need the past 6 months and 12 months average (However, the calculation need to use the months I have in the data, and sometimes there will be less then 6 or 12 months), but I still need to get the average and frequency for it , however, I am not sure how to get it.
I am trying to write a code, but it is not complete and is not working as well, I don't get an error; it just doesn't work.
I am open for Excel formulas as well, the problem may be the last row and that it need to use the data I have to calculate and not 6 and 12 months full.
PS: I post a similar question on https://www.ozgrid.com/forum/index.php?thread/1227312-dynamic-way-to-calculate-the-last-6-months-average/
Will This formulas work for you?
Average for last 6 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-6);B:B)
Frequency for last 6 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=6;COUNTIF(A:A;">="&EDATE(MAX(A:A);-6))/6;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))
Average for last 12 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-12);B:B)
Frequency for last 12 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=12;COUNTIF(A:A;">="&EDATE(MAX(A:A);-12))/12;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))