
¿Es posible utilizarlo datetime2
para calcular el mes anterior a partir de \today
? (Alternativamente otro paquete).
Encontré algunas soluciones que utilizan la deducción negativa de días, pero eso causaría problemas hacia fin de mes.
Ejemplo: \today
= 12-JUL-2017
Resultado buscado: "Hoy es 12 de julio de 2017. El mes pasado fue junio".
Respuesta1
Puede guardar la fecha actual usando \DTMsavenow
y luego usar \DTMfetchmonth
y DTMmonthname
o sus equivalentes para calcular el mes anterior (o siguiente). Agregué una versión \lastmonth
y \nextmonth
.
\documentclass{article}
\usepackage[calc]{datetime2}
\usepackage{ifthen}
\newcommand\lastmonth[1]{\ifthenelse{\DTMfetchmonth{#1}=1}
{\DTMmonthname{12}}{\DTMmonthname{\numexpr\DTMfetchmonth{#1}-1\relax}}}
\newcommand\nextmonth[1]{\ifthenelse{\DTMfetchmonth{#1}=12
{\DTMmonthname{1}}{\DTMmonthname{\numexpr\DTMfetchmonth{#1}+1\relax}}}
\begin{document}
\DTMsavenow{today}
\DTMsavedate{jan20}{2017-01-20}
Today is \today.
Last month was \lastmonth{today}.
Today is \DTMusedate{jan20}.
Last month was \lastmonth{jan20}.
Next month is \nextmonth{jan20}.
\end{document}
Respuesta2
También puedes hacer esto sin ningún paquete. Puedes definir tu propia \lastmonth
macro:
\documentclass{article}
% \setdate{<day>}{<month>}{<year>}
\newcommand{\setdate}[3]{\day=#1\month=#2\year=#3\relax}%
\newcommand{\lastmonth}{%
\ifcase\month\or% 0
December\or % 1
January\or % 2
February\or % 3
March\or % 4
April\or % 5
May\or % 6
June\or % 7
July\or % 8
August\or % 9
September\or % 10
October\or % 11
November\fi % 12
}
\begin{document}
Today is \today.
Last month was \lastmonth.
\setdate{20}{1}{2017}%
Today is \today.
Last month was \lastmonth.
\end{document}