Having a date stored in a macro:


I would like to do some calculations (advance days/months) using datetime2. If \Mydate stores the date in yyyy-mm-dd I won't have issues, but having the date stored in a different style (say dd-mm-yyyy), the command \DTMsavedate will not recognize it and I could not issue


I can use some string manipulations to convert the date stored in the macro to the iso format the \DTMsavedate needs, but I would prefer this conversion be made by the same datetime2 package or a package that manipulates date strings specifying the input style. In the \Mydate example the user should specify the ddmmyyyy format and the separator in the same way as he/she decides to work with the 'mmddyyyy' format.

There is some way to input a date given as dd-mm-yyyy into datetime2 to operate with it?

It would be better if the input format could be specified and the task of converting a date into a format recognized by the package is delegated to the same package.

  • Do you want to be able to use both styles of input, or are you asking how to change datetime2 to operate on the alternative format only? If you want to use both, then conversion with a macro would be simpler; if you want to use just the alternate, then it's not too hard to modify the datetime2 parser.
    – Alan Munn
    Commented Oct 15, 2017 at 21:54
  • In whatever style, @AlanMunn. I would like to have a solution on which the user could give the date in whatever style (supported by datetime2, and by only specifying the style used, the date could be increased by n days and displayed in the style the user choose. This code will be in a class. Commented Oct 15, 2017 at 22:00

2 Answers 2


You can re-define \DTMsavedate like in this code:


   \expandafter \ifx \csname f=\DTMinformat\endcsname \relax
      \message{! \string\DTMsavedate: input format "\DTMinformat" undeclared}%
   \else \csname f=\DTMinformat\expandafter\endcsname#2&{#1}\fi
\def\DTMfdef#1{\expandafter\def\csname f=#1\endcsname}

% input format declarations:
\DTMfdef{yyyy-mm-dd}#1-#2-#3&{\DTMs{#1-#2-#3}}  % iso input





Test1 is \DTMusedate{test1}

Test2 is \DTMusedate{test2}

Test3 is \DTMusedate{test3}

Test4 is \DTMusedate{test4}


The main date parser for inputting dates in datetime2 is \@dtm@parsedate. This can be redefined to use dd-mm-yyyy input, and all other datetime2 commands will work as before. The display style of course is set by datetime2 anyway.

It easy to have datetime2 recognize both input styles in the same document.

Here's an example:

% Make datetime2 use either  dd-mm-yyyy input format or iso format

\DTMsetstyle{ddmmyyyy} % Set output format to match


Today is \DTMusedate{mynow}

December 20th is \DTMusedate{dec20}

October 20th is \DTMusedate{oct20}

October 20th ISO input is \DTMusedate{oct20iso}

\DTMusedate{oct20} is
   the same day as
        \number-\myct\space day\ifnum\myct<-1s\fi\space before
        \number\myct\space day\ifnum\myct>1s\fi\space after
\fi \fi

output of code

  • Thanks! Very clever the automatic swap between year and day when the input is iso or ddmmyyyy. It should work for me in a first approximation to solve the problem or as a workaround. However, I misunderstood your question about the both styles: I won't need both styles at the same compilation like in your example, because cases like mmddyyyy will be impossible to distinguish from other formats. I wish the user has the chance to supply the date following its locale, so for example dd-mm-yyyy, mm-dd-yyyy, dd.mm.yyyy, iso or any format and this date being recognized by datetime2 Commented Oct 16, 2017 at 1:58
  • @EmilioLazo This would require a lot more work, since the locale information is designed only for output format not input format. I guess you could hook into the locale to make different versions of \@dtm@parsedate. What you're asking for is effectively a major feature request for the package.
    – Alan Munn
    Commented Oct 16, 2017 at 2:17
  • Understood... not easy to interpret a date displayed according to all output formats, but a the simplest cases mm-dd-yyyy, dd-mm-yyyy and iso with whatever separator might be implemented in the parser with a new macro to tell the parser which input style should use. Anyone interested? :-) I'll try! Thanks @AlanMunn. Commented Oct 16, 2017 at 2:36
  • @EmilioLazo Well there's enough here to do what you want. Hooking it off of the locale itself would be the most elegant, but would require a lot of delving into the localization code of datetime2. Creating your own input setup macro wouldn't be hard though.
    – Alan Munn
    Commented Oct 16, 2017 at 3:18

You must log in to answer this question.

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