# Date Calculations (output should be the fixed day of the month)

I'm building a LaTeX document that already got the date from other files. The date showed as 23 November 2015 as the example (by \today). Then I need to output another date related to this original date. The resulted date should always the fifteenth day of the next month or this month depending on the original date.

For example, if the original date is 23 November 2015, which is after the 15th day of the month, the resulted date should be 15 December 2015. If the original date is 12 November 2015, the resulted date should be 15 November 2015.

Are there any method to do such calculation with the date output in the file?

• "get the date from other files": how? Is it a timestamp of a file, a textual information in the file? What is the format of the file, can you edit it, according to what standard is the date specified? Is using shell-escape an option? – Clément Nov 23 '15 at 14:51
• The date showed as 'Nov 23 2015. It might be unclear in the question. – Joe Nov 23 '15 at 14:55
• @Joe: How are you printing the current dates? Do you specify them as text strings (say Nov 23 2015, verbatim), or do you use (say) \today? – Werner Nov 23 '15 at 15:40
• @Werner I use \today there – Joe Nov 24 '15 at 4:02

With some conditionals on the current day-of-the-month, advdate can be used to retrieve the date you're looking for:

\documentclass{article}

\newcommand{\nextfifteenth}[1][\relax]{{%
\ifx\relax#1\relax\else\SetDate[#1]\fi% If a date was specified
\today---%
\ifnum\day<15\else% \day >= 15
\fi
\ThisDay{15}% Day will always be 15th
\today
}}

\begin{document}

\today

\nextfifteenth

\nextfifteenth[15/12/2015]

\nextfifteenth[12/12/2012]

\today

\end{document}


You could also supply the date in a textual format 12 December 2012 (say) using the following:

\documentclass{article}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
\str_case:nnF { #1 } {
{ January   } {  1 }
{ February  } {  2 }
{ March     } {  3 }
{ April     } {  4 }
{ May       } {  5 }
{ June      } {  6 }
{ July      } {  7 }
{ August    } {  8 }
{ September } {  9 }
{ October   } { 10 }
{ November  } { 11 }
{ December  } { 12 }
} { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

\def\formattextdate#1 #2 #3\relax{%
\ThisDay{#1}% Set day
\expandafter\ThisMonth\expandafter{\StrMonthToNum{#2}}% Set month
\ThisYear{#3}% Set year
}

\newcommand{\nextfifteenth}[1][\relax]{{%
\ifx\relax#1\relax\else
\expandafter\formattextdate #1\relax%
\fi% If a date was specified
\today---%
\ifnum\day<15\else% \day >= 15
\fi
\ThisDay{15}% Day will always be 15th
\today
}}

\begin{document}

\today

\nextfifteenth

\nextfifteenth[15 December 2015]

\nextfifteenth[12 December 2012]

\today

\end{document}

• Is it possible to use similar method with hand-typing date? For example, \nextfifteenth[12 December 2012]. – Joe Nov 24 '15 at 7:58
• @Joe: See my updated answer. – Werner Nov 24 '15 at 14:01
• If I want the date showed in the format [Day Month Year] like 15 December 2016, are there any easy way? – Joe Apr 15 '16 at 3:21

\documentclass{article}

\def\zz#1{#1 --- \nextXV{#1}}

\def\nextXV#1{\nextXVx#1\relax}

\def\nextXVx#1 #2 #3\relax{%
\ifnum#2<16
#1 15 #3%
\else
\csname nextXV#1\endcsname
\space 15
\expandafter\ifx\csname nextXV#1\endcsname\nextXVDec
\the\numexpr#3+1\relax
\else
#3%
\fi
\fi}

\def\nextXVJan{Feb}
\def\nextXVFeb{Mar}
\def\nextXVMar{Apr}
\def\nextXVApr{May}
\def\nextXVmay{Jun}
\def\nextXVJun{Jul}
\def\nextXVJul{Aug}
\def\nextXVAug{Sep}
\def\nextXVSep{Oct}
\def\nextXVOct{Nov}
\def\nextXVNov{Dec}
\def\nextXVDec{Jan}

\begin{document}

\zz{Nov 12 2015}

\zz{Nov 23 2015}

\zz{Dec 23 2015}

\end{document}
`