11

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.

Werner
  • 603,163

3 Answers3

16

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
}
Sean Allred
  • 27,421
7

Have a look at the isodate package:

\documentclass[english]{article}
\usepackage{babel}
\usepackage{isodate}

\begin{document}

\isodate \today

\end{document}

isodate

Uwe Ziegenhagen
  • 13,168
  • 5
  • 53
  • 93
5

Taking a look at article.cls you find the definition of the macro \today. You could re-define that macro to suit your tastes and even define a new macro (e.g., \mydate) to print in the style you asked the date inserted in the usual format.

\documentclass{article}
\let\oldtoday\today
\def\today{%
  \number\year-\ifcase\month\or%
  Jan\or Feb\or Mar\or Apr\or May\or Jun\or%
  Jul\or Aug\or Sep\or Oct\or Nov\or Dec\fi%
  -\number\day%
}
\newcommand{\mydate}[3]{%
  \number#1-\ifcase#2\or%
  Jan\or Feb\or Mar\or Apr\or May\or Jun\or%
  Jul\or Aug\or Sep\or Oct\or Nov\or Dec\fi%
  -\number#3%
}
\begin{document}
\today{} vs. \mydate{2015}{01}{21}
\end{document}

output of code

Pier Paolo
  • 2,790
  • Back at this one. Have looked at all the suggestions, very good for all but they are all talking about printing the '\today' date. What I am printing is a variable with a date in it: ie: I am getting totally mixed up how to just change the month part from 05 or MAY.

    As the great man said, if you think the solution is simple, then obviously you do not understand the total problem.

    – Gregory West May 06 '15 at 15:53
  • @GregoryWest: I didn't really understand your comment above, sorry. If it meant that your problem is solved, please consider upvoting this answer and/or marking it as “accepted”. If your problem is still not solved, please rephrase your original question adding a clear example of what you have and what you would like to get. – Pier Paolo May 06 '15 at 17:13
  • No still have the problem. Here is what I am trying to solve. I have a latex report form being called from a program which also passes a lot of data down to the form. There are about 8 different dates involved. Right now the dates are printing in the form of 2015-05-07 but the client wants them to print in the form 2015-MAY-07. What I was hoping for (I can dream can't I) a function like \alphadate{<%deliverydate%>} which would also work on \alphadate{<%duedate%>} \alphadate{<%shipdate%>} etc. Does this make any more sense? – Gregory West May 07 '15 at 18:31
  • @GregoryWest: It is still not clear enough to me. (A suggestion: If you edit your original question, this post will be put back to the main page of the site where many more users will read it (and possibly answer it), while right now it's just me that still follows this post.) – Pier Paolo May 07 '15 at 19:16
  • Here is one of the lines of Latex code printing a date: – Gregory West May 07 '15 at 22:07
  • Payment due NET <%terms%> Days from date of Invoice. Interest on overdue amounts will acrue at the rate of 2% per month starting from <%duedate%> until paid in full. Items returned are subject to a 10% restocking charge. – Gregory West May 07 '15 at 22:07
  • I need the field <%duedate%> to print out in the format 2015-MAY-07 all the time. Right now I can only get 2015-05-07 which is not acceptable. – Gregory West May 07 '15 at 22:08