Comparing dates in Excel - excel

I'm trying to compare date in excel to return a value.
I want to see...
if date of birth + today() is less than DATE(2021,9,1) years then output 6
if date of birth + today() is less than DATE(2021,9,1) years then output 5
if date of birth + today() is less than DATE(2021,9,1) years then output 4
if date of birth + today() is less than DATE(2021,9,1) years then output 3
This is for a school project. I'm struggling to understand the date concept.
Can someone point me in the right direction?
Thanx for reading,
Sam

This should work, imagining that cell A1 contains the date of birth:
=IF((A1 + today()) < DATE(2021,9,1), 6)
=IF((A1 + today()) < DATE(2021,9,1), 5)
=IF((A1 + today()) < DATE(2021,9,1), 4)
=IF((A1 + today()) < DATE(2021,9,1), 3)

After going home and sleeping on it, I think I figured it out. I wanted my date to be static and check against pupil date of births to determine their age for a particular year. I already have a version of this which is dynamic and updates itself when the sheet is loaded. Here is what I did, where E3 is equal to a pupils date of birth...
=IF(E3="","",
IF(DATEDIF(E3,DATE(2021,9,1),"Y")+1>=11,6,
IF(DATEDIF(E3,DATE(2021,9,1),"Y")+1>=10,5,
IF(DATEDIF(E3,DATE(2021,9,1),"Y")+1>=9,4,
IF(DATEDIF(E3,DATE(2021,9,1),"Y")+1>=8,3,
"")))))
If there is an easier way to accomplish this I would relish feedback.
Thanx again to those of you who replied,
Sam
This is my dynamic version
=IF(E3="","",MAX(0,DATEDIF(DATE(YEAR(E3+122),9,1),NOW(),"Y" )-4))
This calculates age
=IF(E3="","",
DATEDIF(E3,TODAY(),"Y") & "." &
DATEDIF(E3,TODAY(),"YM") & "." &
DATEDIF(E3,TODAY(),"MD")
)

In Excel, a date is stored as a number of days since 1/1/1900 (or 1904, not very important), you can subtract date2-date1 and that will return the number of days between the 2 dates.
With that logic, a simple formula to get your age from your birth date stored in E3 would be:
=INT((TODAY()-E3)/365)
Just for fun, enter a date in a cell, then Clear Formats. You will see the number of days.

Related

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

I have the Year, Week-of-Year and Day-of-the-Week as follows:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture.
I tried many formulas, but I am sure there might be an easy one.
I think you are counting the weeks starting from zero because for 9/1/2022 (YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)). In order to use the logic to multiply the number of weeks by 7. You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date, the Sunday of the first week of the year, not the first day of the year (YYYY/1/1)
Here is the approach we use in cell E2:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))
We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year (fDay) was not a Sunday:
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))
The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1.
Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.
Notice that the year 2021 started on a Friday, so if we want to find a day for the first week (0) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
IF(YEAR(result) <> y, "ERROR: Date from previous year", result))
I found the solution:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6
I found this solution, but you need to do further testing if it really works.
I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)
And then make date using: =DATE(A2;C2;E2)

Select records with at one day in centain year

I want to make a selection of records where date range (start date - end date) includes at least 1 day of 2020. So for example: if a record has 2018-01-13 as the start date and 2020-01-09 as end date, it has to be included in my selection, as there is at least one day active in 2020.
Sample data:
How can I achieve this?
Just check the years with the standard between notation
Start Year <= 2020 <= End Year
=AND(YEAR(A1)<=2020,YEAR(B1)>=2020)
=IF(OR(YEAR(A2)=2020,YEAR(B2)=2020),1,0)
Excel store dates as numbers, so, you must compare them like numbers
Make a column with: =IF(OR(B2 < $C$2; $D$2 < A2); 0; 1)
where
A: start date
B: final date
C: 2020-01-01
D: 2020-12-31
, Now you can use only values 1

Create a rolling date list in Excel

I want to create a rolling list of dates in Excel like so:
Day Date
Day 1 01-Jul-19
Day 1 02-Jul-19
Day 1 03-Jul-19
Day 1 04-Jul-19
Day 1 05-Jul-19
Day 1 06-Jul-19
Day 1 07-Jul-19
Day 2 02-Jul-19
Day 2 03-Jul-19
Day 2 04-Jul-19
Day 2 05-Jul-19
Day 2 06-Jul-19
Day 2 07-Jul-19
Day 2 08-Jul-19
Day 3 03-Jul-19
Day 3 04-Jul-19
Day 3 05-Jul-19
Day 3 06-Jul-19
Day 3 07-Jul-19
Day 3 08-Jul-19
Day 3 09-Jul-19
Day 4 04-Jul-19
. .
. .
. .
So essentially what's happening is that the 7-day range moves forward by one day each time, from a specific start date (in the example above, 01-07-19) until it reaches an end date. Is there an automated way of doing this?
#ashvin10 you can do this in vba, but you can also accomplish this with 2 formulas without using vba at all, here's how:
for illustration purposes we'll just assume you are starting with 07/01/2019 on the first row and your information will be displayed in columns A and B.
in A1 enter the string Day 1
in B1 enter your starting date, like 07/01/2019
in A2 enter this formula: ="Day " & IF(MOD(ROW(A2),7)<>0, MID(A2,5,(LEN(A2)-4)), MID(A2,5,(LEN(A2)-4))+1)
in B2 enter this formula: =IF(A2=A1,B1+1,OFFSET(B2,-7,0)+1)
highlight cells A2 and B2
click on the cross that becomes available on the bottom right hand corner of cell B2
drag down the formula till you hit the end date you desire
the cells are populated with the values you requested in the format you requested
If you absolutely have to have it done using vba please let me know and I can show you how to do it that way as well, but this way is much easier.
EDIT: #ashvin10 I'm so sorry, the original formula I instructed you to put into A2 only works for Day 1 through Day 9, if you go into days past 9 it won't display correctly. I've fixed the formula that should be pasted into A2 so now it will work no matter how many days you go down. I'm so sorry for the confusion.
Alternatively, this can also be done in Python.
import datetime
start_date = '01-07-2019'
end_date = '31-01-2020'
output_file_name = 'rolling dates'
output_file_extension = '.CSV'
delimiter = '\t'
with open((output_file_name + output_file_extension.lower()), 'w+') as file:
header = "Day" + delimiter + "Date" + '\n'
file.write(header)
start_date_object = datetime.datetime.strptime(start_date, '%d-%m-%Y').date()
end_date_object = datetime.datetime.strptime(end_date, '%d-%m-%Y').date()
number_of_days = abs((end_date_object - start_date_object).days)
next_day = start_date_object
for i in range(1, number_of_days + 2):
for j in range(7):
file.write(("Day {0}" + delimiter + next_day.strftime('%d-%m-%Y') + '\n').format(i))
next_day += datetime.timedelta(days=1)
start_date_object += datetime.timedelta(days=1)
next_day = start_date_object
After running the code above, I simply created a blank Excel file and then imported the data from the CSV file output by this code.
This is arguably more complicated than #gharbad-the-weak's answer but thought I'd include this anyway.

Change the date to fiscal year

I want to write a function in Excel to change the date. The logic is like this: if the month is (Jan, Feb or March) the result show me one year past (-1 year) and if the month is (April to -December) the result show the current year (which year the date shows).
example: if date is 02,Jan,2012 the result show me 2011 else show me 2012.
=IF(MONTH(G3) >=4, YEAR(G3), YEAR(G3) - 1) where G3 is the date to test, is one way.
Please try:
=IF(OR(MONTH(A1)=1,MONTH(A1)=2,MONTH(A1)=3),2011,2012)
With 02-Jan-2012 in A1 try,
=YEAR(A1)-(MONTH(A1)<4)
For a full date use one of these,
=DATE(YEAR(A1)-(MONTH(A1)<4), MONTH(A1), DAY(A1))
=EDATE(A1, -(MONTH(A1)<4)*12)
To extract fiscal year use:
=YEAR(A1) + IF(MONTH(A1)>=4,1,0)
I think in your case you would need:
=YEAR(A1) - IF(MONTH(A1)>=4,0,1)
If the months is before 4th month then subtract 1 year, else keep the same year. I wouldn't convert it to a full date DD/MM/YYYY with a 1 year subtracted, to avoid confusion keep it as year only YYYY.
Already plenty of answers, but thought I'd throw another one up:
=YEAR(DATE(YEAR(A1),MONTH(A1)-3,DAY(A1)))

Working Hours Excel Formula

I need to calculate the working hours elapsed between two dates and times, for example:
Holiday taken between 01/09/2014 and 05/09/2014
5 working days # 8 hours per day.
I need the result to show me how many working hours that would be. For example:
ANNUAL ENTITLEMENT: 89.9 Hours
DATE FROM DATE TO RETURN TO WORK HOURS REQUIRED HOURS REMAINING DATE
01/09/2014 05/09/2014 06/09/2014 40 49.90
I have no idea if this is even possible!
I am assuming these are given cells. if you type in the date to a cell you can click on a new cell and put uptop by the fx this
for example. In C1 you can type this into the fx. Make sure you put the equal sign.
=B1-A1
This is what is the dates in the cells
A1 = 1/9/2014
B1 = 5/9/2014
This will give you 120 which is the total days inbetween.
You will want to get the number of weeks so you can divide by 7.
You will multiply weeks by number of days worked which is 5
Then you want the weeks times 8 hours you can do this in C1
=(B1 - A1)/7 * 5 * 8
which gives you 685.7143
you need to also take into account weekends which a simple subtraction will not do. Firstly find the total number of days:
TOTAL_NUMBER_DAYS = B1-A1 + 1
then calculate how many weekends:
TOTAL_WEEKENDS = WEEKNUM(B1) - WEEKNUM(A1)
finally take the total days and subtract weekends:
NET_TOTAL_DAYS = TOTAL_NUMBER_DAYS - (TOTAL_WEEKENDS * 2)
TOTAL_HOURS = NET_TOTAL_DAYS * 8
I solved this recently and had a working solution initially in Excel 2013. Slightly adapted to work in 2007 (lack of 'Days()' function). We use it for reporting on support tickets (length of time between opening and closing a ticket).
Inputs are "Workday start", "Workday end" from which "Hours worked" and "Hours not worked" are calulated.
Hours worked =HOUR(WorkEnd-WorkStart)+(MINUTE(WorkEnd)+MINUTE(WorkStart))/60
Hours not worked = 24 - Hours worked
Further inputs are a table ("Data") with first two columns "Open" and "Close", which are dateserial cells.
Next column is standard numeric value "Gross hours" =(Data[[#This Row],[Close]]-Data[[#This Row],[Open]])*24
Next column is standard numeric value "Net workdays" =NETWORKDAYS(Data[[#This Row],[Open]],Data[[#This Row],[Close]])
Next column is standard numeric value "Net days" =MAX(1,DAY(Data[[#This Row],[Close]])-DAY(Data[[#This Row],[Open]]))
Next column is standard numeric value "Gross workhours" =IF(Data[[#This Row],[Gross hours]]>0,Data[[#This Row],[Gross hours]]-24*(Data[[#This Row],[Net days]]-Data[[#This Row],[Net workdays]]),0)
Last column is standard numeric value "Total work hours" =IF(Data[[#This Row],[Gross workhours]]<24,Data[[#This Row],[Gross workhours]],Data[[#This Row],[Gross workhours]]-(HoursNotWorked*Data[[#This Row],[Net workdays]]+HoursWorked))
This solution makes it trivial to adjust start and end work times, as well as accounting for any holidays (via a small change to the NETWORKDAYS() function to utilise the optional parameter).

Resources