是否可以datetime
按照以下格式返回当前时间和日期?
2017 年 2 月 25 日,星期六,下午 1:24:01(欧洲中部时间)。
答案1
使用datetime2
并创建一个新的样式:
\documentclass{article}
\usepackage[calc,english]{datetime2}
% Date/time style:
% <dow> <dayofmonth>. <monthname> <yyyy>, <hh>:<mm>:<ss> <ampm> CET
% Example: Saturday 25. February 2017, 01:24:01 PM CET
\DTMnewdatestyle{mydatestyle}{%
% \DTMdisplaystyle{<YYYY>}{<MM>}{<DD>}{<dow>}
\renewcommand{\DTMdisplaydate}[4]{%
\DTMsettimestyle{mytimestyle}% Set the time style
\DTMweekdayname{##4}% Day of week
\space
##3% Day of month
. % Period
\DTMmonthname{##2}% Month of year
\space
##1% Year
, % Comma
\DTMcurrenttime
}%
\renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}
\DTMnewtimestyle{mytimestyle}{%
% \DTMdisplaytime{<hh>}{<mm>}{<ss>}
\renewcommand{\DTMdisplaytime}[3]{%
\def\THEHOUR{##1}\def\THEAMPM{AM}% Assume AM
\ifnum##1>12
\edef\THEHOUR{\number\numexpr##1-12}% Correct for...
\edef\THEAMPM{PM}% ...PM
\fi
\DTMtwodigits{\THEHOUR}% <hh>
:%
\DTMtwodigits{##2}% <mm>
:%
\DTMtwodigits{##2}% <ss>
\space
\THEAMPM% AM/PM
\space
CET% CET
}%
}
\begin{document}
\DTMsetdatestyle{mydatestyle}
Today is \today.
\end{document}
答案2
这与en-GB
提供的样式非常相似datetime2-english
:
\documentclass{article}
\usepackage[en-GB]{datetime2}
\newcommand*{\DTMenGBzonemaps}{%
\DTMdefzonemap{01}{00}{CET}%
}
\DTMenGBzonemaps
\DTMlangsetup[en-GB]{
mapzone,
ord=omit,
daymonthsep={.\space},
datetimesep={,\space}}
\begin{document}
\DTMnow
\end{document}
这不完全匹配,但非常接近:
am/pm 部分可以轻松转换为大写,方法如下:
\renewcommand{\DTMenglishampmfmt}[1]{\MakeUppercase{#1}}
时间englishampm
样式不支持秒或两位数小时。我真的不建议使用 am/pm 样式的两位数小时,因为粗略一看可能会将前导零解释为表示二十四小时格式,而不会注意到 am/pm 部分。如果您确实需要它,您可以重新定义样式,englishampm
如下所示:
\DTMrenewtimestyle
{englishampm}% label
{%
\renewcommand*\DTMdisplaytime[3]{%
\ifnum##2=0
\ifnum##1=12
\DTMtwodigits{##1}\DTMenglishtimesep
\DTMtwodigits{##2}\DTMenglishtimesep
\DTMtwodigits{##3}%
\space
\ifnum##3=0
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishnoon}}%
{\DTMenglishnoon}%
\else
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishpm}}%
{\DTMenglishpm}%
\fi
\else
\ifnum##1=0
\DTMtwodigits{##1}\DTMenglishtimesep
\DTMtwodigits{##2}\DTMenglishtimesep
\DTMtwodigits{##3}%
\space
\ifnum##3=0
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishmidnight}}%
{\DTMenglishmidnight}%
\else
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglisham}}%
{\DTMenglisham}%
\fi
\else
\ifnum##1=24
00\DTMenglishtimesep
\ifnum##3=0
\DTMtwodigits{##2}\DTMenglishtimesep
\DTMtwodigits{##3}%
\space
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishmidnight}}%
{\DTMenglishmidnight}%
\else
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglisham}}%
{\DTMenglisham}%
\fi
\else
\ifnum##1<12
\DTMtwodigits{##1}\DTMenglishtimesep
\DTMtwodigits{##2}\DTMenglishtimesep
\DTMtwodigits{##3}%
\space
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglisham}}%
{\DTMenglisham}%
\else
\DTMtwodigits{\numexpr##1-12}\DTMenglishtimesep
\DTMtwodigits{##2}\DTMenglishtimesep
\DTMtwodigits{##3}%
\space
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishpm}}%
{\DTMenglishpm}%
\fi
\fi
\fi
\fi
\else
\ifnum##1<13
\ifnum##1=0
12%
\else
\DTMtwodigits{##1}%
\fi
\DTMenglishtimesep\DTMtwodigits{##2}%
\DTMenglishtimesep\DTMtwodigits{##3}%
\space
\ifnum##1=12
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishpm}}%
{\DTMenglishpm}%
\else
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglisham}}%
{\DTMenglisham}%
\fi
\else
\DTMtwodigits{\number\numexpr##1-12}%
\DTMenglishtimesep\DTMtwodigits{##2}%
\DTMenglishtimesep\DTMtwodigits{##3}%
\space
\ifnum##1=24
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglisham}}%
{\DTMenglisham}%
\else
\DTMtexorpdfstring
{\DTMenglishampmfmt{\DTMenglishpm}}%
{\DTMenglishpm}%
\fi
\fi
\fi
}%
}%
然后需要重置样式:
\DTMsettimestyle{englishampm}
这种样式定义比 Werner 答案中的样式有优势,因为它是完全可扩展的,所以如果你添加hyperref
并使用:
\begin{document}
\tableofcontents
\section{\DTMnow}
\end{document}
那么时间将正确显示在 PDF 书签中。
答案3
我认为 Werner 的精彩回答并不完全正确。一天中从 00:00:00 到 00:59:59 的时间写为 00:09:23,所有 ISO 都将其写为 12:09:23 am;一天中从 12:00:00 到 12:59:59 的时间写为 12:09:23,所有 ISO 都将其写为 12:09:23 pm。因此,Werner 代码的正确版本应如下所示
\DTMnewtimestyle{mytimestyle}{%
% \DTMdisplaytime{<hh>}{<mm>}{<ss>}
\renewcommand{\DTMdisplaytime}[3]{%
\def\THEHOUR{##1}\def\THEAMPM{AM}% Assume AM
\ifnum##1>12
\edef\THEHOUR{\number\numexpr##1-12}% Correct for...
\edef\THEAMPM{PM}% ...PM
\fi%
\ifnum##1=12
\edef\THEHOUR{12}% Correct for...
\edef\THEAMPM{PM}% ...PM
\fi%
\ifnum##1=0
\edef\THEHOUR{12}% Correct for...
\edef\THEAMPM{AM}% ...AM
\fi%
\THEHOUR% <hh>
:%
\DTMtwodigits{##2}% <mm>
:%
\DTMtwodigits{##2}% <ss>
\space
\THEAMPM% AM/PM
\space
CET% CET
}%
}
答案4
以下代码可以在OpTeX中使用:
\setweekday \setminutes \setseconds % Initializes \weekday, \hours, \minutes, \seconds.
% Note that TeX primitives \day, \monts, \year are initialized by default.
\def\printweekday{\ifcase\weekday Sun\or Mon\or Tues\or Wednes\or Thurs\or Fri\or Satur\fi day}
\def\printdate{%
\printweekday~\the\day. \cs{_mt:m\the\month:en} \the\year,
\printhour:\Othe\minutes:\Othe\seconds~\AMorPM~CET
}
\def\printhour{% We distinguish between PM and AM
\ifnum\hours>12 \tmpnum=\numexpr\hours-12 \def\AMorPM{PM}%
\else \tmpnum=\hours \def\AMorPM{AM}\fi
\Othe\tmpnum
}
Today is \printdate.
\bye