
是否可以使用datetime2
來計算上個月\today
? (或另一個包)。
我找到了一些使用負扣除天數的解決方案,但這會在月底左右引起問題。
範例:\today
= 2017 年 7 月 12 日
想要的輸出:“今天是 2017 年 7 月 12 日。上個月是 6 月。”
答案1
您可以使用保存當前日期\DTMsavenow
,然後使用\DTMfetchmonth
和DTMmonthname
或其等效值來計算上個月(或下個月)。我已經添加了 a\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}