1
$\begingroup$

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$

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I used y = 0 as an example I can work with. In real y can be between 0 and 6. y depends on my reference date. I have the reference date but how should a date be expressed in an equation? For my example it seems it is in fact x - 6. I need a more general solution which would work regardless of the reference date (-> ordinality) and indexPath.Item. Do you have another idea how I can do this? $\endgroup$ – testing Mar 17 '15 at 11:28
  • $\begingroup$ @testing: If $y$ can vary, you need to explain how it should influence the desired answer, not merely assert that it does. $\endgroup$ – Henning Makholm Mar 17 '15 at 12:32
  • 1
    $\begingroup$ If you express the reference date on the same timescale as the $x$ input, your formula is simply $x-\mathit{referenceDate}$. $\endgroup$ – Henning Makholm Mar 17 '15 at 12:33
  • 1
    $\begingroup$ @testing: You're saying that if $y=6$ then the start is a Saturday, but you're not saying anything about which Saturday you want to be the reference day. There are at least 52 of them each year to choose from, and many years too. And just in the two-week calendar example in your question there are two different Saturdays that could both equally well be your desired reference date, as far as your explanations go. $\endgroup$ – Henning Makholm Mar 17 '15 at 13:15
  • 1
    $\begingroup$ @testing: So would it work for you to say that the number of the reference date is $$ R = \begin{cases} 6 & \text{if }y=0 \\ y-1 & \text{otherwise}\end{cases} $$ ? $\endgroup$ – Henning Makholm Mar 17 '15 at 13:52
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.