1

Having a date stored in a macro:

\def\Mydate{20-12-2017}

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

\DTMsavedate{testdate}{\MyDate}

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.

2
  • 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

4

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

\documentclass{article}
\usepackage[calc]{datetime2}

\let\DTMsaved=\DTMsavedate
\def\DTMsavedate#1#2{%
   \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}
\def\DTMs#1#2{\DTMsaved{#2}{#1}}
\def\DTMinformat{yyyy-mm-dd}

% input format declarations:
\DTMfdef{yyyy-mm-dd}#1-#2-#3&{\DTMs{#1-#2-#3}}  % iso input
\DTMfdef{dd-mm-yyyy}#1-#2-#3&{\DTMs{#3-#2-#1}}
\DTMfdef{yyyymmdd}#1#2#3#4#5#6#7#8&{\DTMs{#1#2#3#4-#5#6-#7#8}}
\DTMfdef{ddmmyyyy}#1#2#3#4#5#6#7#8&{\DTMs{#5#6#7#8-#3#4-#1#2}}
\DTMfdef{dd.mm.yyyy}#1.#2.#3&{\DTMs{#3-#2-#1}}

\begin{document}
\def\DTMinformat{yyyy-mm-dd}
\DTMsavedate{test1}{2017-12-20}

\def\DTMinformat{dd-mm-yyyy}
\DTMsavedate{test2}{20-10-2017}

\def\DTMinformat{yyyymmdd}
\DTMsavedate{test3}{20171020}

\def\DTMinformat{dd.mm.yyyy}
\DTMsavedate{test4}{21.5.2017}

Test1 is \DTMusedate{test1}

Test2 is \DTMusedate{test2}

Test3 is \DTMusedate{test3}

Test4 is \DTMusedate{test4}

\end{document}
0
4

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:

\documentclass{article}
\usepackage[calc]{datetime2}
\newcount\myct
% Make datetime2 use either  dd-mm-yyyy input format or iso format
\makeatletter
\@ifpackageloaded{datetime2-calc}
{\def\@dtm@parsedate#1-#2-#3\@dtm@endparsedate{%
    \ifnum#1>31\relax
        \pgfcalendardatetojulian{#1-#2-#3}{\@dtm@julianday}%
    \else
        \pgfcalendardatetojulian{#3-#2-#1}{\@dtm@julianday}%
    \fi
        \pgfcalendarjuliantodate{\@dtm@julianday}{\@dtm@year}{\@dtm@month}{\@dtm@day}%
        \pgfcalendarjuliantoweekday{\@dtm@julianday}{\count@}%
        \edef\@dtm@dow{\number\count@}%
}}
{\def\@dtm@parsedate#1-#2-#3\@dtm@endparsedate{%
\edef\@dtm@month{\number#2}%
\ifnum#1>31\relax
  \edef\@dtm@year{\number#1}%
  \edef\@dtm@day{\number#3}%
\else
  \edef\@dtm@day{\number#1}%
  \edef\@dtm@year{\number#3}%
\fi
  \def\@dtm@dow{-1}%
}}
\makeatother

\DTMsetstyle{ddmmyyyy} % Set output format to match

\begin{document}
\DTMsavenow{mynow}
\DTMsavedate{dec20}{20-12-2017}
\DTMsavedate{oct20}{20-10-2017}
\DTMsavedate{oct20iso}{2017-10-20}

Today is \DTMusedate{mynow}

December 20th is \DTMusedate{dec20}

October 20th is \DTMusedate{oct20}

October 20th ISO input is \DTMusedate{oct20iso}

\DTMsaveddatediff{oct20}{dec20}{\myct}
\DTMusedate{oct20} is
\ifnum\myct=0
   the same day as
\else
   \ifnum\myct<0
        \number-\myct\space day\ifnum\myct<-1s\fi\space before
    \else
        \number\myct\space day\ifnum\myct>1s\fi\space after
\fi \fi
\DTMusedate{dec20}
\end{document}

output of code

4
  • 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 .