## LaTeX forum ⇒ Text Formatting ⇒ Date Arithmetic or Calculation Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

### Date Arithmetic or Calculation

My wife supplies me with material for a minister's manuscript booklet that I print prior to each Sunday. Usually, the booklet is printed several days (or even a week or two) in advance of the Sunday. I do not have much use for \today as it is not the day on which I print the stuff that matters, but the date on which she uses it.

As certain parts of the service vary little from week to week, but still need to be kept fresh, such as the short prayer for blessing the offering, she has a selection which I have stored in a stylesheet (.sty) from which I pick a different one for each Sunday of the month. I call the \newcommands names like \offerone, \offertwo, etc.

I would like to automate this, so that (if possible) LaTeX or a package should calculate which Sunday of the month it is, and output the appropriate prayer. For instance, if it were the third Sunday of the month, I would want it to pick \offerthree.

What I would like to be happen is this: I input the date of the Sunday (e.g. 4 August 2019), and assign that to some variable with which I can work. I would then divide the day_of_the_month part (i.e. 4) by 7 (which would give 0), and add 1 to the result. That would enable me to select the \offerone for the first Sunday of August.

The parts that I do not know how to do are:
(a) how to get LaTeX to pick up on what the Sunday date is if I tell it, for instance, \date{2019-08-04}. How can I fetch the 04 into a variable, please?
(b) I would then need to divide that variable by 7, and add 1, to calculate which Sunday of the month it was.
I think I know how to do the logic for selecting the correct prayer.

Maybe what I am asking is not possible using LaTeX alone. However, any advice that anyone can give me would be appreciated, as my lack of knowledge as to how to grab the day-number part of the date is hampering me.
Thanks for reading this far. If you can even help me to express my question better, I would appreciate that too.
Thanks,
Calum

Ijon Tichy
Posts: 123
Joined: Mon Dec 24, 2018 10:12 am
Some easy arithmetics:

Note, you will get a strange TeX error message, if the argument of \date is not an iso-date.

Note, \samedayofweek can be used like a TeX counter (which is not the same like a LaTeX counter). So you have to prefix it with \the to use it's arabic representation, as I've done to print the result. If you want to test, whether or not a date is a Sunday, you can use scrdate's \ISODayNumber, which expects an already expanded argument. So you cannot use \ISODayNumber{\thisdate} but have to use \expandafter\ISODayNumber\expandafter{\expanded{\thisdate}}.
Last edited by Ijon Tichy on Tue Jul 23, 2019 12:31 pm, edited 1 time in total.

Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm
This is wonderful. I shall test it out later on today, and report back. I can see the suggestions opening up so many options for me. Thanks meantime.

Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm
Thanks again for the reply, which I found very useful, although it has taken me all day to learn quite what is going on in the code. I have now gotten to the stage that I can put in a date, and it will display it on the front cover, and state which corresponding day of the month it is. I am using \datetime2 package.


As I do booklets for every second Wednesday morning, the above code works for that too Thanks for that bonus.

I really appreciate this help. The answer I accept, because it dealt with the questions that I was raising.