我希望能够通过命令(例如\longtoday
和 )输入短版本或长版本\shorttoday
。短日期的更多示例是 12 May 17 和 01 Jan 07,长日期相同,除了写出的月份和年份,例如 12 May 2017 和 01 January 2007。
我原本想使用isodate
,但不知道它的效果如何。有人能帮忙吗?谢谢!
答案1
您可以使用 来完成datetime2
。
\documentclass{article}
\usepackage{datetime2}
\DTMusemodule{english}{en-GB}
\DTMnewdatestyle{long}{%
\renewcommand{\DTMdisplaydate}[4]{%
\number##3~\DTMenglishmonthname{##2}~\number##1\relax
}%
\renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}
\DTMnewdatestyle{short}{%
\renewcommand{\DTMdisplaydate}[4]{%
\number##3~\DTMenglishshortmonthname{##2}~\number##1\relax
}%
\renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}
\newcommand{\longtoday}{{\DTMsetdatestyle{long}\today}}
\newcommand{\shorttoday}{{\DTMsetdatestyle{short}\today}}
\begin{document}
\longtoday
\shorttoday
\end{document}
答案2
下面应该这么做。
\documentclass[]{article}
\newcommand\defdate[3]{% Just for testing purpose
\day=#1\relax
\month=#2\relax
\year=#3\relax
\ignorespaces}
\def\JayLaoneShortyear#1#2#3\relax{%
#3}
\def\JayLaoneDayTWODIGITS#1#2\relax{%
\if\relax\detokenize{#2}\relax
0#1%
\else
#1#2%
\fi}
\newcommand*{\shorttoday}{%
\expandafter\JayLaoneDayTWODIGITS\number\day\relax\space
\ifcase\month
\or Jan%
\or Feb%
\or Mar%
\or Apr%
\or May%
\or Jun%
\or Jul%
\or Aug%
\or Sep%
\or Oct%
\or Nov%
\or Dec\fi
\space
\expandafter\JayLaoneShortyear\number\year\relax
}
\newcommand*{\longtoday}{%
\expandafter\JayLaoneDayTWODIGITS\number\day\relax\space
\ifcase\month
\or January%
\or February%
\or March%
\or April%
\or May%
\or June%
\or July%
\or August%
\or September%
\or October%
\or November%
\or December\fi
\space
\number\year
}
\begin{document}
\noindent
\defdate{1}{12}{2018}
\shorttoday\\
\longtoday\\
\defdate{7}{1}{2010}
\shorttoday\\
\longtoday\\
\defdate{17}{1}{2009}
\shorttoday\\
\longtoday
\end{document}
答案3
另一种(但更短)的方法是datetime2
:
\documentclass{article}
\usepackage[en-GB]{datetime2}
\newcommand{\longtoday}{{\DTMlangsetup[en-GB]{abbr=false,ord=omit}\today}}
\newcommand{\shorttoday}{{\DTMlangsetup[en-GB]{abbr,ord=omit}\today}}
\begin{document}
\longtoday
\shorttoday
\end{document}
如果日期需要出现在 PDF 书签中,则需要在分段命令之前设置样式。例如:
\documentclass{article}
\usepackage[en-GB]{datetime2}
\usepackage{hyperref}
% default setting:
\DTMlangsetup[en-GB]{abbr=false,ord=omit}
\begin{document}
\DTMlangsetup[en-GB]{abbr}
\section{\today}
\DTMlangsetup[en-GB]{abbr=false}
\section{\today}
\end{document}
对于任何其他可扩展上下文也是如此,例如将日期写入外部文件。
如果要确保月份中的两位数日期(没有序数后缀):
\renewcommand*{\DTMenglishordinal}[1]{\DTMtwodigits{#1}}