About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.


7.1 The datetime2 Package

The datetime2 package has replaced the now obsolete datetime package. This section is just a brief introduction, see the user guide [101] for other commands and settings not described here.

The simplest use of this package is:

\documentclass{article}
\usepackage{datetime2}
\begin{document}
This PDF was created on \today.
\end{document}

This produces

This PDF was created on 2015-09-08.

This is the default date format. If you want the full date, time and time zone, you can use

\DTMnow

instead of \today. This will display the date and time in the form

2015-09-08 11:17:58+01:00

Note that XeLaTeX doesn't provide the time zone information so you will need to use PDFLaTeX or LuaLaTeX if you want this.

If you want to use a regional format, you can specify the region in the package option. For example:

\documentclass{article}
\usepackage[en-GB]{datetime2}
\begin{document}
This PDF was created on \DTMnow.
\end{document}

This produces:

8th September 2015

If you want to pick up the regional setting from babel you can use the useregional option:

\documentclass[british]{article}
\usepackage{babel}
\usepackage[useregional]{datetime2}
\begin{document}
This PDF was created on \DTMnow.
\end{document}

If you prefer a numeric regional format you can use useregional=numeric instead.

You must additionally install the appropriate language module, for example, datetime2-english [100] to enable the regional support. If you want to display the day of the week name, you can use the showdow package option, but this isn't available for all regions. You will need to check the documentation for the relevant module to find out if it's supported.

If you want to display a particular date (without the time) in the current style you can use

\DTMdate{date}

where ⟨date⟩ is given in the format yyyy⟩-⟨mm⟩-⟨dd. For example:

\DTMdate{2015-09-08}

You can save a date and time for later use with:

\DTMsavetimestamp{name}{data}

where ⟨name⟩ is a unique label that identifies this date and time, and ⟨data⟩ is in the format

YYYY⟩-⟨MM⟩-⟨DD⟩T⟨hh⟩:⟨mm⟩:⟨ss⟩⟨zone

where ⟨YYYY⟩ is the year, ⟨MM⟩ is the month number, ⟨DD⟩ is the day of the month, ⟨hh⟩ is the hour (24), ⟨mm⟩ is the minute value, ⟨ss⟩ is the second value and ⟨zone⟩ is the time zone, which may be either Z or in the format TZh⟩:⟨TZm where ⟨TZh⟩ is the hour offset and ⟨TZm⟩ is the minute offset. The argument ⟨data⟩ may also be a control sequence that expands (one level) to the required format.

Alternatively, you can just save the date with:

\DTMsavedate{name}{date}

where the ⟨date⟩ is in the format YYYY⟩-⟨DD⟩-⟨MM and ⟨name⟩ is again a label.

A previously saved date and time can be displayed in the current style using:

\DTMuse{name}

Just the date can be displayed using:

\DTMusedate{name}

and just the time with:

\DTMusetime{name}

In all cases, ⟨name⟩ is the label identifying the date or time stamp.

Example 37. Displaying Dates and Times (datetime2 package)

Here's an example that uses the en-GB style:

\documentclass{article}

\usepackage[en-GB]{datetime2}

\newcommand*{\DateStamp}{2015-11-28T20:13:04Z}
\DTMsavetimestamp{mydate}{\DateStamp}
\DTMsavetimestamp{mydate2}{2014-06-01T09:01:58+01:00}

\begin{document}
Now: \DTMnow.

Saved: \DTMuse{mydate}; \DTMuse{mydate2}.

\end{document}

This produces

Now: 8th September 2015 11:25am BST.

Saved: 28th November 2015 8:13pm GMT; 1st June 2014 9:01am BST. End of Image.


You can download or view this document.

Be careful if you are using babel with the shorthands on as this may change the category code of characters such as : and - which will cause the date or time parsing in commands like \DTMsavetimestamp to fail. You may need to temporarily switch off the shorthands. See the babel manual [7] for further details.


This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs