About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account

Custom Date Formatting (pgfcalendar.sty)

\documentclass{article}

\usepackage{etoolbox}
\usepackage{pgfkeys,pgfcalendar}

\newcount\julianday
\newcount\dayofweek

\newcommand*{\ord}[1]{%
  \number#1%
  \ifcase#1\or st\or nd\or rd\or th\or th\or th\or th\or th\or
  th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or
  th\or th\or st\or nd\or rd\or th\or th\or th\or th\or th\or
  th\or th\or st\fi
}

\newcommand*{\printdate}[1]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \pgfcalendarjuliantodate{\julianday}{\thisyear}{\thismonth}{\thisday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  % Now display the date:
  \datefmt[\dayofweek]{\thisyear}{\thismonth}{\thisday}%
}

\newcommand*{\datefmt}[4][]{%
  \ifstrempty{#1}
  {}% day of week missing
  {%
    \pgfcalendarweekdayname{#1}\space
  }%
  \ord{#4}~\pgfcalendarmonthname{#3} \number#2%
}

\renewcommand*{\today}{%
  \pgfcalendardatetojulian{\year-\month-\day}{\julianday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  \datefmt[\dayofweek]{\year}{\month}{\day}%
}

\begin{document}

Today: \today.
Tomorrow: \printdate{\year-\month-\day+1}.
Yesterday: \printdate{\year-\month-\day+-1}.

The first day of this month: \printdate{\year-\month-1}.
The last day of this month: \printdate{\year-\month-last}.

A specific date: \printdate{2014-3-20}.

A date without the day of week: \datefmt{2014}{3}{20}.

\end{document}

Download pgfprintdate.tex or pgfprintdate.pdf.

© 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