2

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?

4
  • (I don't get why people want to use more/less packages. Does copying the source code of the package into the document count?)
    – user202729
    Mar 24, 2022 at 14:32
  • 1
    Also, you most likely don't want to redefine LaTeX commands (such as \today), it may mess up some packages.
    – user202729
    Mar 24, 2022 at 14:33
  • 1
    @user202729 Thanks for your comment about \today. About your fisrt comment, imho, limiting the number of packages helps prevent future package incompatibilities. When working on large documents with lots of packages it can also make a major difference in terms of readability. But here my concern was not to limit the number of packages but I thought that if there was a datetime2 it was perhaps to replace the datetime package... I should have specified that in my question sorry.
    – zetyty
    Mar 24, 2022 at 14:49
  • Hm reading the documentation it looks like that datetime2 is indeed intended to replace datetime.
    – user202729
    Mar 24, 2022 at 14:51

1 Answer 1

2

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}
1
  • Just perfect! Thanks a lot!
    – zetyty
    Mar 25, 2022 at 9:45

You must log in to answer this question.

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