
datetime2
から前月を計算するために使用することは可能ですか\today
?(または別のパッケージ)。
マイナスの日数控除を使用する解決策もいくつか見つかりましたが、月末に問題が発生します。
例: \today
= 12-JUL-2017
必要な出力: 「今日は 2017 年 7 月 12 日です。先月は 6 月でした。」
答え1
と を使用して現在の日付を保存し、 と またはそれに相当するものを使用して前月 (または翌月) を計算できます。\DTMsavenow
との両方のバージョンを追加しました。\DTMfetchmonth
DTMmonthname
\lastmonth
\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}
答え2
パッケージなしでこれを行うこともできます。独自の\lastmonth
マクロを定義することもできます。
\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}