Use two different date formats: yymmdd and dd/mm/yyyy

datedatetimedatetime2formatting

I would like to be able to use, in the same document, these two date formats:

  • dd/mm/yyyy (as default format)
  • yymmdd (as "local" format)

Why I want that?

Because I would like to use the "local" format to define the document reference code (e.g. mydocumenttitle_yymmdd.pdf) but in "normal" text, I would like to use the default dd/mm/yyyy format with the command \today.

First attempt

For the moment I wasn't able to do better than this:

\documentclass{article}
\usepackage{datetime}
\newdate{specialdate}{\day}{\month}{\year}
\usepackage[datesep={}]{datetime2} 

\begin{document}
Format with dd/mm/yyyy: {\ddmmyyyydate\displaydate{specialdate}}

Format with yymmdd: \today
\end{document}

Which gives the following result:

enter image description here

But there are the following problems:

  1. The yymmdd format is not correct because I achieve only yyyymmdd. How to obtain the date format with only two digits for the year: yymmdd?

  2. The use of command \today doens't give the default format but the yymmdd one.
    Is there a way to define \today with the default format (i.e. dd/mm/yyyy) and another command for the "local" format (i.e. yymmdd)?

  3. It use the two packages datetime and datetime2.
    Is it possible to use only one package?

Best Answer

Try this code.

b

The package datetime is obsolete. Only datetime2 is needed.

Two styles have been defined to present the desired date formats --default (ddmmyyyyx) and local (yymmdd)--.

Using \DTMsetstyle{<name>} in the preamble sets the default style.

\documentclass{article}

\usepackage{datetime2}  

    
\DTMnewstyle{yymmdd}{%
\renewcommand*\DTMdisplaydate[4]{%
\DTMtwodigits{##1}\DTMtwodigits{##2}\DTMtwodigits{##3}% 
}
\renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}
{}{}{}

\newcommand{\daymonthyearsep}{/}    % define the separator
\DTMnewstyle{ddmmyyyyx}{%
\renewcommand*\DTMdisplaydate[4]{%
\DTMtwodigits{##3}\daymonthyearsep\DTMtwodigits{##2}\daymonthyearsep\number##1%
}%
\renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}
{}{}{}  


\DTMsetstyle{ddmmyyyyx}% set as default style

\begin{document}

Default style \emph{ddmmyyyyx}: \today
\bigskip

Now a local style \emph{yymmdd}: {\DTMsetstyle{yymmdd} \today}%local style <<<<<<<<<<<<<<<<
\bigskip

Default style again \emph{ddmmyyyyx}:   \today
    
\end{document}
Related Question