Calculate difference in days to reference date

666 Views Asked by At

This question is related to a programming question. Because I think it's pure mathematic I post this question here.

I want to calculate the difference in days to a reference point. Therefore I have two inputs:

  • indexPath.Item: natural number between zero and infinite
  • ordinality: natural number between zero and six, which represents a day in a week

ordinality:

0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday

I made a sketch to demonstrate my task:

calculate difference in days

As you can see I have a reference date - the 1st of Feb. 2015. This day is a Sunday and the ordinality for Sunday is 0. I want to calculate the difference in days to this date in dependency of the ordinality and the indexPath.Item.

I can't figure out how a equation would look like under these conditions. I could set up an input/output table as follows:

x = indexPath.Item
y = ordinality
z = difference in days

y=0 x=10 z=4
y=0 x=9 z=3
y=0 x=8 z=2
y=0 x=7 z=1
y=0 x=6 z=0
y=0 x=5 z=-1
y=0 x=4 z=-2
y=0 x=3 z=-3
y=0 x=2 z=-4
y=0 x=1 z=-5
y=0 x=0 z=-6

This is only an example, where y is always zero.

The reference date in my case is always the first of a month. The 1st of a month falls into a week - namely the first week of a month (the 1st week of a month doesn't always include a full week in my case!). The 1st of a month defines the ordinality (y) and this is the start (or the shift if you want) of my calculations. Here the difference in days is zero. If y = 0, Sunday (last column) is the start. If y = 6, Saturday (next-to-last column) is the start. In x direction the difference in days increases and in -x direction the difference in days decreases (negative values).

Can anyone give me a hint in the right direction?

Edit:

I found out that 1 - y + x would work, but here another ordinality (y) is used: Monday: 1 - Sunday: 7. So this formula doesn't work for y = 0. Can this be expressed in an adapted formula?

Solution:

Thanks to Henning Makholm!

$R = \begin{cases} 6 & \text{if }y=0 \\ y-1 & \text{otherwise}\end{cases}$

$x−referenceDate$

3

There are 3 best solutions below

11
On BEST ANSWER

It looks like your desired output is simply $x-6$. Your example doesn't show anything about how you want $y$ to influence the output, because all rows in the example have $y=0$.

In any case, saying that you want to know the distance from some Sunday doesn't tell anything about which Sunday that is. What you need as an input is the complete date of the reference day (ideally expressed on your "indexPath" scale) -- not merely which day of the week it is.

0
On

This is not an answer to the question as it is asked

The problem of the number of days between two given dates is probably one of the difficult problems I had to face until somebody suggested something simple to me. So, I pass the trick to you.

Convert the reference date and the current date to Julian dates and subtract.

You could find the code in C from Numerical Recipes on pages 13-14 at http://apps.nrbook.com/c/index.html

May be (I hope) this could give you another way.

0
On

Please search for date difference on math.stackexchange , Here are some relevant questions with answers: Subtracting two dates

Formula to calculate difference between two dates.

If you are interested in knowing the day of the week for a particular date. You can study Zeller's congruence