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.

2
  • 1
    What kind of data do you have? And check out the datetime package. Commented Jan 21, 2015 at 17:58
  • 2
    If you want to avoid confusion, you should use the standard format YYYY-MM-DD; see the answer by Uwe. Commented Jan 22, 2015 at 1:28

3 Answers 3

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
}
1
  • 1
    You would have to ask OP. I imagine you can change it. I've added a way to patch. Commented Jan 21, 2015 at 19:37
7

Have a look at the isodate package:

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

\begin{document}

\isodate \today

\end{document}

isodate

1
  • 1
    But he doesn't want an ISO date. Commented Jan 23, 2015 at 1:56
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

7
  • 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: <shipdate> 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. Commented May 6, 2015 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
    Commented May 6, 2015 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? Commented May 7, 2015 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
    Commented May 7, 2015 at 19:16
  • Here is one of the lines of Latex code printing a date: Commented May 7, 2015 at 22:07

This site is temporarily in read-only mode and not accepting new answers.

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