1

How do I customize date and time display for the compile date and time? I see the datetime2 package gives many predefined options, but what if I want to do something unique? The datetime2 mentions the \DTMnewstyle command. But I can't understand how to use it.

Examples:

10:15am | Mon Jan 02, 2023 % Time before date. Lower case "am" with no space. Separated by pipe. Three letter day. Three letter month. Two digit day. Four digit year.

Jan/02/23 % Elements separated by "/". Three letter month, two digit day, two digit year. Not useful for anything except helping to know how to customize.

10:15:00 , Monday, January 02, 2023 % Time with seconds separated by colons. No "am", "pm" distinctions. Comma separator. Full day of week. Full Month. Two digit day. Comma. Four digit year.

01#02#2023 % Two digit day. Two digit month. Four digit year. "#" delimiter.

23%01%02 & Two digit year. Two digit month. Two digit day. "%" delimiter. This adds the complexity of using the "%" character.

My actual use case is for a unique time stamp in the header of my draft documents. But I want to customize how it looks. Understanding how to customize, will also allow me and others more flexibility. The combined examples are an attempt at the date/time equivalent of The quick brown fox jumps over the lazy dog which I hope the community finds useful.

1 Answer 1

1

You've not shown, what you problem using \DTMnewstyle is. So here are two examples to explain, how \DTMnewdatestyle is used:

\documentclass{article}

\usepackage[yearmonthsep=\%,monthdaysep=\%]{datetime2}
\DTMnewdatestyle{Twodigits}{%
  \renewcommand*{\DTMdisplaydate}[4]{%
    \DTMtwodigits{##1}% use exact (last) two digits of the year
    \DTMsep{yearmonth}% add the yearmonth separator given by package option yearmonthsep
    \DTMtwodigits{##2}\DTMsep{monthday}% similar for month
    \DTMtwodigits{##3}% similar for day
  }%
}
\DTMnewdatestyle{TwodayTwomonthFouryear}{%
  \renewcommand*{\DTMdisplaydate}[4]{%
    \DTMtwodigits{##3}\DTMsep{monthday}% start with day
    \DTMtwodigits{##2}\DTMsep{yearmonth}% similar for month
    \number ##1\relax% year should always be with four digits so use it as is
  }%
}

\DTMsetdatestyle{Twodigits}%
  
\begin{document}
\DTMtoday

\DTMsetdatestyle{TwodayTwomonthFouryear}
\setkeys{datetime2.sty}{yearmonthsep=\#,monthdaysep=\#}
\DTMtoday
\end{document}

enter image description here

If you want to use \DTMnewstyle instead, you not only have to redefine \DTMdisplaydate (in second argument), but also \DTMdisplaytime (in third argument), \DTMdisplayzone (in fourth argument), and \DTMdisplay (in fifth argument). The arguments all of these commands are explained in section 3 of the manual.

A command to show the current time would be, e.g., \DTMcurrenttime. A command to show everything would be, e.g., \DTMnow. These are also documented in section 3 of the manual.

For more examples see the manual. You can find the definition of all styles in the “Code” section of the manual. For the name of the day or month also datetime2-calc could be useful.

2
  • I could not understand the documentation either. The examples you've given here were very useful, and do not appear to be in the manual.
    – Niles
    Commented Oct 10, 2023 at 16:41
  • 1
    @Niles This is exactly why I showed these examples, even though the question lacked any MWE. But I had to wait more than a year for the first upvote. Thank you.
    – cabohah
    Commented Oct 10, 2023 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .