Text FormattingDate Arithmetic or Calculation

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

Date Arithmetic or Calculation

Post by Pangur »

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Date Arithmetic or Calculation

Post by Ijon Tichy »

Some easy arithmetics:
\documentclass{article}
\usepackage{scrbase}% For \XdivY
\usepackage{scrdate}% only for \ISODayName

\makeatletter
\newcommand*{\dayofisodate}[1]{%
  \expandafter\@dayofisodate\expanded{#1}\@nil
}
\newcommand*{\@dayofisodate}{}
\def\@dayofisodate#1-#2-#3\@nil{#3}
\newcommand*{\thisdate}{\@date}
\renewcommand*{\today}{\the\year-\the\month-\the\day}
\makeatother
\newcommand*{\samedayofweek}[1]{%
  \numexpr \XdivY{\numexpr\number\dayofisodate{#1}-1\relax}{7} + 1\relax
}
\newcommand*{\isodayname}[1]{%
  \expandafter\ISODayName\expandafter{\expanded{#1}}%
}
\begin{document}
\thisdate{}
is the number \the\samedayofweek{\thisdate}
\isodayname{\thisdate} of the month.

\date{2019-08-04}
\thisdate{}
is the number \the\samedayofweek{\thisdate}
\isodayname{\thisdate} of the month.

Or for all day of august 2019:

\makeatletter
\day=1
\@whilenum \day<31 \do {%
  \date{2019-08-\the\day}%
  \thisdate{}
  is the number \the\samedayofweek{\thisdate}
  \isodayname{\thisdate} of the month.\par
  \advance\day by \@ne
}
\makeatother
\end{document}
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.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Date Arithmetic or Calculation

Post by Pangur »

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

Date Arithmetic or Calculation

Post by Pangur »

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.

\newcommand*{\pubdate}{2019-08-25} [color=#BFBF00]% Specify the publication date.[/color]

\DTMdate{\pubdate} [color=#BFBF00]% Show publication date as Sunday 25th August 2019.
[/color]
\renewcommand*{\thisdate}{\pubdate} [color=#BFBF00]% Make \thisdate same as publication date.[/color]
is the
number \the\samedayofweek{\thisdate} \isodayname{\thisdate} [color=#BFBF00]% number 4 Sunday[/color]
of the month.


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.
Post Reply