112

Currently I put the date in my document using: \date{\today}

How do I display the date in the YYYY-MM-DD format?

1
  • 4
    See here ctan.org/pkg/datetime From the documentation (pg 10) we have The option yyyymmdd makes \today produce YYYY/MM/DD date
    – Sigur
    Commented Jan 4, 2014 at 13:30

7 Answers 7

91

edit: As noted by @Sean Allred, datetime has been superseded by datetime2, which defaults to YYYY-MM-DD.

Using the package datetime with the option yyyymmdd as

\usepackage[yyyymmdd]{datetime}

you just change the value of \dateseparator to replace the default / by - (or -- if you want).

\renewcommand{\dateseparator}{--}

Also as noted by @Vincent, you can define your own date format.

enter image description here

8
  • 4
    Somebody should check whether ISO 8601 says that the separator has to be - or --. (iso.org/iso/home/standards/iso8601.htm). Definitely YYYY/MM/DD should not be used - that would violate the standard. Keith
    – KeithB
    Commented Jan 8, 2014 at 9:53
  • 2
    @KeithB, Wikipedia claims, “The separator used between date values (year, month, week, and day) is the hyphen”. Also, it’s unlikely the standard mandates characters encoded neither in EBCDIC nor ASCII. Commented Jan 13, 2014 at 19:20
  • 14
    note: datetime has been superseded by datetime2 Commented Apr 6, 2015 at 17:38
  • 1
    According to the CTAN page, datetime is now obsolete and has been replaced by datetime2. Commented Mar 19, 2018 at 14:54
  • 1
    @KeithB for separation: tl;dr: simply - as in „The separator used between date values (year, month, week, and day) is the hyphen, while the colon is used as the separator between time values (hours, minutes, and seconds).“0. Original documents unfortunately are behind a paywall, some libraries will have free access though.
    – vv01f
    Commented Jun 11, 2019 at 9:12
53

My solution needs no packages. The only thing you need to know is that the primitive registers \day, \month and \year include the desired information:

\def\mydate{\leavevmode\hbox{\the\year-\twodigits\month-\twodigits\day}}
\def\twodigits#1{\ifnum#1<10 0\fi\the#1}

The date in my format: \mydate.
5
  • 2
    worked just fine in 2019. thanks! Commented Aug 7, 2019 at 12:55
  • 3
    You can also use the internal \two@digits macro, which is defined as \two@digits=macro: #1->\ifnum #1<10 0\fi \number #1.
    – user94293
    Commented Apr 26, 2020 at 9:44
  • 3
    @user94293 No, I can't use such macro, because it is defined in the latex.ltx file. But I don't load such file.
    – wipet
    Commented Apr 26, 2020 at 9:56
  • I like this, but what if we want to write the full name of the month instead of the \twodigits format? Commented Sep 1, 2023 at 21:54
  • 1
    @KennethOdle TeX provides \ifcase command. You can use \ifcase\month\or January\or February\or March\or ...\or November\or December\fi.
    – wipet
    Commented Oct 16, 2023 at 16:02
37

Use the package datetime2

\usepackage[style=iso]{datetime2}
5
  • 9
    This should be an edit to the other answers using datetime since datetime is now superseded and should no longer be used for new documents. Commented Apr 6, 2015 at 17:55
  • 7
    datetime2 Error: 'iso' is not a recognised dialect.
    – crypdick
    Commented Feb 22, 2017 at 18:55
  • 1
    sorry but I do not see how this package is useful. It seems to force you to manually enter the date values, which it then formats.... so why not just manually write them yourself in the correct format in the document? What is this accomplishing? This answer does not actually show how to use the datetime2 to solve the problem in the question. Commented Aug 31, 2017 at 2:22
  • 4
    The package option should be style=iso, but a numeric YYYY-MM-DD style is the default anyway, so it's not needed. Commented Jun 7, 2018 at 11:30
  • 4
    @user5359531 \documentclass{article}\usepackage{datetime2}\begin{document}\today\end{document} is a basic MWE that prints the current date as YYYY-MM-DD style. As for specific dates, the syntax is \DTMdate{2018-06-07} but the formatting depends on the style. So while it does just display 2018-06-07 for the default style, if you change the style it will display the date in a different format. Commented Jun 7, 2018 at 11:34
26

According to the link of Sigur:

\usepackage{datetime}
\newdateformat{specialdate}{\THEYEAR-\twodigit{\THEMONTH}-\twodigit{\THEDAY}}
\date{\specialdate\today}
17

Use the package isodate

\usepackage[iso,german]{isodate}

It offers various options (and needs one of its language options) and commands to change the date format.

2
  • 1
    This does not answer "How do I display the date in the YYYY-MM-DD format?" and would be more useful with an actual code sample. Commented Aug 31, 2017 at 2:26
  • It seems to change how \today works if you import it after datetime.
    – EL_DON
    Commented Oct 8, 2019 at 18:07
6

Recently (May, 2018), Donald P. Goodman III created the new package texdate.

From the package documentation:

It can print dates, advance them by numbers of days, weeks, or months, determine the weekday automatically, and print them in (mostly) arbitrary format. It can also print calendars (monthly and yearly) automatically, and can be easily localized for non-English languages.

For example, the YYYY-MM-DD format of today is:

\documentclass{article}
\usepackage{texdate}
\begin{document}
\initcurrdate
\printfdate{ISOext}
\end{document}

enter image description here

0

I found an easier way to get today's date in YYYY-MM-DD format:

\newcommand\twodigits[1]{%
   \ifnum#1<10 0#1\else #1\fi
}

\number\year-\twodigits{\number\month}-\twodigits{\number\day}

Dear Mary,

Today I have received what I need to tell you;
I was taking a walk and I finally know what advice to give.
In the words of the great Kanye West, 
\textit{"If you have the opportunity to play this game called life, 
you have to appreciate every moment."} 
Keep striving toward success, and that is what you will become.


Also linux $>$ windows.

Best, \\
John Doe

Result:

enter image description here

2
  • You could probably simplify \twodigits by just conditioning for the prefix: \newcommand{\twodigits}[1]{\ifnum#1<10 0\fi#1}. Also, your output doesn't include the -s.
    – Werner
    Commented Mar 1, 2022 at 19:23
  • @Werner I forgot to create a .jpg that reflected the dashes in the above code; It is only the result pic that doesn't show the dashes. When you execute the code the dashes are present.
    – Alex Angel
    Commented Mar 27, 2022 at 18:08

You must log in to answer this question.

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