2

Background:

I've been trying to find a way to get LaTeX to automatically calculate the number of days and weeks (and even years for the sake of completeness) elapsed between a predefined date and \today.

MWE:

I have an overleaf MWE line 28

Question:

Is there a way for LaTeX to calculate the number of days, weeks, and years between a predefined date and \today?

Sample:

Assuming \today is 2022JUN17

DATE: 2022JUN22 - `\today`     OUTPUT: 5 days
DATE: 2022JUL25 - `\today`     OUTPUT: 5 weeks and 3 days
DATE: 2025JUL25 - `\today`     OUTPUT: 3 years, 6 weeks and 1 day

1 Answer 1

3

The pgf calendar package has such a utility. It can convert dates in to a "Julian" integer, and then one only need to compute differences between these integers for different dates.

\documentclass{article}
\usepackage{pgfcalendar}
\newcount\myjuliandate
\newcount\myjuliantoday
\newcommand{\DaysTo}[3]{%
\pgfcalendardatetojulian{\year-\month-\day}{\myjuliantoday}%
\pgfcalendardatetojulian{#1-#2-#3}{\myjuliandate}%
\advance\myjuliandate by-\myjuliantoday\relax
\the\myjuliandate
}
\begin{document}
\DaysTo{2022}{07}{25}

\DaysTo{2022}{08}{28}
\end{document}
2
  • Is there way to show this as years, weeks, days?
    – 3kstc
    Jun 22, 2022 at 23:21
  • @3kstc It is possible but a bit cumbersome. Imagine you are using this in a leap year. Let's say it is February 28 and you want to know how far in the future February 28th of the following year is. Probably you want to hear "1 year". However, if you ask on February 29th how far in advance March 1st of the following year is, would you still say "1 year" or "1 year and 1 day"? Obviously one can distinguish all these cases but this is a lot of efforts, and regardless of how one implements it, there will be some who won't agree with the outcome.
    – user272921
    Jun 23, 2022 at 2:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .