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'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.
I made a formula in excel, yet when trying it out, it responds with NAME?
=IF(MAAND(VANDAAG()) > MAAND(D1); (NETTO.WERKDAGEN(D1; D3)) * D5; D6)
My goal is to calculate how many Hours I still need This Month in Row 8.
depending on startdate(startdatum and current date (huidige datum) (not enddate(einddatum)) and how many work days there are between those dates times the hours.
Any ideas on how to solve this?
UPDATE
translations:
Maand = Month
Vandaag = Today
Netto.WerkDagen = NETWORKDAYS
Solved. I had to use ALS (dutch translation).....
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)))
I'm working on an Assignment where 2 different Date Ranges are available. I'm trying to add their differences and formatting them in XX Years, XX Months, XX Days (where XX is the Sum Number).
Now for a single range, I've searched on internet and Found DATEDIF Function useful like in Below image:
Formula
=DATEDIF(Start_Date_1,End_Date_1,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")&" Days"
It works Fine for 1 Date Range. But I've 2 different Ranges and 2 Different Differences. So I've tried to Sum the Each Parameter (Year, Month & Days) in the above formula Like Below:
Formula:
=DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")&" Days"
Now the Formula works fine until the Months/Days Range exceed 12/31 Respectively. As Seen in the picture, The months range has summed up to 13 while Days to 32. Now I'm looking for any solution or even Different approach to do the same formatting. Any Solution or Suggestion will be highly appreciated.
Thanks in Advance :).
If you define a month as 30 days you could just nestle IF's to compensate:
=IF(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")>30,IF((DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")+1)>11,(DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")+1)&" Years, "&(DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")-11)&" Months, "&(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")-30)&" Days",(DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y"))&" Years, "&(DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")+1)&" Months, "&(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")-30)&" Days"),DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")&" Days")
So IF days are over Thirty we check if months+1 are over 11, if both yes we minus 30 from days, 11 from months (11 to compensate a year plus the month from days) and add 1 to years, if months+1 aren't over 11 we simply add one to months and minus 30 from days, if neither are over the limits then we display the unadulterated formula.
(using your images as an example:
=IF(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")>30,IF((DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")+1)>11,(DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y")+1)&" Years, "&(DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")-11)&" Months, "&(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")-30)&" Days",(DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y"))&" Years, "&(DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")+1)&" Months, "&(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")-30)&" Days"),DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y")&" Years, "&DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")&" Months, "&DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")&" Days")
Will work for you)