Q:

Equivalent of `newdateformat` in `datetime2`?

The datetime package has recently been withdrawn and replaced by datetime2. Any idea for the equivalent of newdateformat in datetime2 ?

A:

With datetime, a new date format was defined using
\newdateformat{〈name〉}{〈format〉}

Within 〈format〉you could use \THEDAY, \THEMONTH and \THEYEAR to reference the day, month and year number. You could also use \twodigit to convert a number to two digits. For example:
\newdateformat{dashdate}{%
  \twodigit{\THEDAY}-\twodigit{\THEMONTH}-\THEYEAR}

With the new datetime2 package, a new date format is defined using:
\DTMnewdatestyle{〈name〉}{〈definition〉}

Here the 〈definition〉part should redefine the two commands \DTMdisplaydate and \DTMDisplaydate. Both these commands have four arguments which represent the year, month, day and day of week numbers. If the day of week number is -1 the style should ignore it, but a style may also ignore the day of week regardless of its value. Instead of \twodigits, we now have \DTMtwodigits which will display its (numerical) argument with exactly two digits. (So it will add a leading zero if the absolute value is less than 10 or it will trim a number if the absolute value is greater than 99.) There are also two other commands you might find useful when defining a date style: \DTMcentury and \DTMdivhundred. There was no direct equivalent in the old datetime package for those two commands.
So to convert our old dashdate example to the new datetime2 format:
\DTMnewdatestyle{dashdate}{%
  \renewcommand*{\DTMdisplaydate}[4]{\DTMtwodigits{##3}-\DTMtwodigits{##2}-##1}%
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}

Here ##3 is the third argument of \DTMdisplaydate (the day), ##2 is the second argument of \DTMdisplaydate (the month) and ##1 is the first argument of \DTMdisplaydate (the year). The fourth argument is ignored. Since the date style is numeric \DTMDisplaydate is set to the same as \DTMdisplaydate as the style is the same regardless of whether or not a date occurs at the start of a sentence.
If you want a textual date style, you can use additional commands provided by the relevant language module. For example, if you have loaded the English module you can use commands such as \DTMenglishmonthname or \DTMenglishordinal. (These commands are described in the "Base Code (datetime2-english-base.ldf)" section of the module's documentation.)
So the datetime manual's example:
\newdateformat{usvardate}{%
  \monthname[\THEMONTH] \ordinaldate{\THEDAY}, \THEYEAR}

can be converted to datetime2 using:
\DTMnewdatestyle{usvardate}{%
  \renewcommand*{\DTMdisplaydate}[4]{\DTMenglishmonthname{##2} \DTMenglishordinal{##3}, ##1}%
  \renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}

This style will only work if the datetime2-english module is loaded otherwise the commands \DTMenglishmonthname and \DTMenglishordinal will be undefined. Since English month names always start with a capital letter, \DTMDisplaydate is again set to \DTMdisplaydate. For languages where this isn't the case then \DTMDisplaydate would need to start with a capital letter. For example, with the datetime2-french module, you can use \DTMfrenchMonthname for the month name starting with a capital and \DTMfrenchmonthname for the lower case month name.
Finally, with datetime, \newdateformat{〈name〉}{〈format〉} created a command called \〈name〉 (so \dashdate in the first example and \usvardate in the second example) which can be used to switch to that format, but with datetime2 the date format is set using \DTMsetdatestyle{〈name〉} (so \DTMsetdatestyle{dashdate} in the first example and \DTMsetdatestyle{usvardate} in the second example).

