[Tex/LaTex] How to print out dates in YYYY-MMM-DD format

datetime

Is there an easy way to print out dates in the format of 2014-Jan-15. Being in North America, Canada not the USA, there are too many date formats in use. To remove any chance of confusion the client would like to use YYYY-MMM-DD.

I can do my program creating the latex PDF is running on debin Linux and not Windows.

Best Answer

Use the datetime package:

\documentclass{article}

\makeatletter
\newcommand\FormatDate[3]{%
  #1-\shortmonthname[#2]-\two@digits{#3}}
\makeatother

\usepackage{datetime}
\newdateformat{gregdate}{\FormatDate{\THEYEAR}{\THEMONTH}{\THEDAY}}

\newcommand\Demonstrate[3]{%
  Date #1-#2-#3 formats as \FormatDate{#1}{#2}{#3}.\par}
\setlength\parindent{0pt}

\begin{document}
\Demonstrate{2015}{01}{01}
\Demonstrate{2015}{02}{02}
\Demonstrate{2015}{03}{03}
\Demonstrate{2015}{04}{04}
\Demonstrate{2015}{05}{05}
\Demonstrate{2015}{06}{06}
\Demonstrate{2015}{07}{07}
\Demonstrate{2015}{08}{08}
\Demonstrate{2015}{09}{09}
\Demonstrate{2015}{10}{10}
Today is {\gregdate\today}.
\end{document}

output


If you wish to rename months, you can redefine \shormonthnameenglish to suit your needs. (You can patch other macros as used by babel, if necessary; see dt-austrian.def, for example.)

\renewcommand*{\shortmonthnameenglish}[1][\month]{%
  \@orgargctr=#1\relax
  \ifcase\@orgargctr
  \PackageError{datetime}{Invalid Month number \the\@orgargctr}{Month
  numbers should go from 1 (jan) to 12 (dec)}%
  \or Jan%
  \or Feb%
  \or Mar%
  \or Apr%
  \or May%
  \or Jun%
  \or Jul%
  \or Aug%
  \or Sept%
  \or Oct%
  \or Nov%
  \or Dec%
  \else%
  \PackageError{datetime}%
  {Invalid Month number \the\@orgargctr}%
  {Month numbers should go from 1 (jan) to 12 (dec)}%
\fi
}