Excel Datedif with dates prior to 1900 - excel

How can I calculate the difference between two dates in Excel older than 1900? For example between 05/05/1862 and 04/16/1938. Thank you very much.

Excel stores dates as number of days since 1900, so one option might be to convert them to dates after 1900. For example, if A1 contains the text 05/05/1862, then this formula results in 05/05/3862:
= LEFT(A1, 6) & RIGHT(A1, 4) + 2000
and the difference in number of days between two text dates can be:
= (LEFT(A1, 6) & RIGHT(A1, 4) + 2000) - (LEFT(A2, 6) & RIGHT(A2, 4) + 2000)

Related

Comparing dates in 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.

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.

Divide revenue across quarters in two years - Excel and SmartSheet

I need a formula to:
recognize the quarter for each date
divide the revenue in half for the start and end quarter.
Below is an example of the results I'm looking for. Need this to be a formula I can build in SmartSheet and/or Excel and transfer over to a SmartSheet.
Add a "helper row" with quarter number and year so you can use a SUMIFS/2
=(SUMIFS($[Est Revenue]#row, $[Start Date]#row, IFERROR((ROUNDUP(MONTH(#cell) / 3) + "" + YEAR(#cell)), 0) = [Q1 2020]$1) + SUMIFS($[Est Revenue]#row, $[End Date]#row, IFERROR((ROUNDUP(MONTH(#cell) / 3) + "" + YEAR(#cell)), 0) = [Q1 2020]$1)) / 2
The helper row would be quarter number before the 4 digit year. So 12020 would be quarter 1 2020.

Convert quarter-year to MM/DD/YYYY in Excel

How to convert this quarter-year format into MM/DD/YYYY in Excel where quarter will convert to first day of quarter. For example,
Q1-2014 to 1/1/2014,
Q2-2015 to 4/1/2015,
Q3-2016 to 7/1/2016,
Q4-2017 to 10/1/2017
Try
=DATE(RIGHT(A1,4),(MID(A1,2,1)*3)-2,1)
That will return a date. Format to display in whatever date format you want.
Here is a formula you could use
=DATE(MID(A1,4,4),IF(MID(A1,1,2)= "Q1", 1, IF(MID(A1, 1, 2) = "Q2", 4, IF(MID(A1,1,2)="Q3", 7, 10))), 1)
Assumptions
The format is Q[1/2/3/4]-YYYY
Formula
Take the last 4 digits as Year
Take Month as 1 if Q1 else 3 if month is Q3,...
Take Date as 1

Week number to Month number

I have a date with this format : 14w01 (year : 2014 week number : 1)
I want to convert this date in month like this : 14m01
Is there a function which converts a week number in a month number ?
Maybe something like this (in vba, not in formula) :
Format(weekNumber, "mm")
Thank you
It depends on how the weeks are defined. One way is to say that the first day of week#1 of a year is 1 January of that year. For this definition, a typical UDF is:
Public Function MonthFromDt(s As String) As Integer
Dim yr As Integer, wk As Integer, d As Date
ary = Split(s, "w")
yr = CInt(ary(0)) + 2000
wk = ary(1)
MonthFromDt = Month(DateSerial(yr, 1, 1) + 7 * (wk - 1))
End Function
There are other definitions of week number.
The DateFormat function is quiet comfortable, however the DateValue function, which parses a date, won't probably support your week format.
I suggest a trick with DateAdd, as DateAdd can handle weeks.
First split your date in year and week number:
Dim parts
parts = Split("2014w33", "w")
Dim year
Dim week
year = CInt(parts(0))
week = CInt(parts(1))
Then, add both to a "zero-date" to add up to the final date. Note that if you give "0" as year for DateAdd, VBA compiler interprets 2000.
dim DateResult
DateResult = dateAdd("yyyy", (year - 2000), DateValue("Jan 1, 0"))
Debug.Print dateResult
DateResult = dateAdd("ww", week, dateResult)
Debug.Print dateResult
Then show the result reformatted:
Debug.Print Format(DateResult, "yyyy\mm")
This prints on my side:
01.01.2014
20.08.2014
2014m08
August 2014, there is week 33 if I look up in the calendar. Seems correct.
I found a way to do it without VBA (and only using Formulas). This assumes A1 contains the "14w01" format
=LEFT(A1,2)&"m"&TEXT(MONTH(DATE(20&LEFT(A1,2),1,1)+(RIGHT(A1,2)*7)),"00")
Heres a breakdown of what the code does..
LEFT(A1,2) returns "14" (year)
MONTH(DATE(20&LEFT(A1,2),1,1)+(RIGHT(A1,2)*7)) converts the week # to the month # and it takes in the year 20&LEFT(A1,2) as well as week # RIGHT(A1,2)
TEXT(...,"00") pads the month # with a 0 if necessary (i.e. 3 becomes 03)
Then we just combine everything together to get "14m01"

Resources