About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-12-13: Ebook sale (12th December 2024 – 1st January 2025): cybercrime fiction short stories Unsocial Media and Smile for the Camera free; crime fiction short story The Briefcase free; short story noir crime fiction I’ve Heard the Mermaid Sing free; crime fiction novel The Private Enemy US$1.99; illustrated children’s story The Foolish Hedgehog US$0.99.

Custom Date and Time Package

Here's the code for mycustomdatetime.sty:
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mycustomdatetime}[2014/03/20 1.0 My custom date and time format]

\RequirePackage{etoolbox}%
\RequirePackage{pgfkeys,pgfcalendar}

% Time commands:

\newcommand*{\timefmt}[4]{%
  \two@digits{#1}:\two@digits{#2}%
  \ifstrempty{#3}% test for empty 3rd argument
  {}% no seconds specified
  {:\two@digits{#3}}% seconds
  \timezonefmt{#4}% time zone
  \relax
}

\newcommand*{\timezonefmt}[1]{%
  \edef\thistimezone{#1}%
  \ifdefempty{\thistimezone}%
  {}% empty argument
  {%
     \expandafter\@timezonefmt\thistimezone\@endtimezonefmt%
  }%
}

\def\@timezonefmt#1:#2\@endtimezonefmt{%
  \ifnum #2=0\relax
    \ifnum #1=0\relax
      Z%
    \else
      \ifnum #1<0 $-$\two@digits{-#1}\else +\two@digits{#1}\fi
    \fi
  \else
    \ifnum #1<0$-$\two@digits{-#1}\else +\two@digits{#1}\fi
    :\two@digits{#2}%
  \fi
}

\def\parsepdfdatetime#1:#2#3#4#5#6#7#8#9{%
  \def\theyear{#2#3#4#5}%
  \def\themonth{#6#7}%
  \def\theday{#8#9}%
  \parsepdftime
}

\def\parsepdftime#1#2#3#4#5#6#7\endparsepdfdatetime{%
  \def\thehour{#1#2}%
  \def\theminute{#3#4}%
  \def\thesecond{#5#6}%
  \ifstrequal{#7}{Z}
  {%
    \def\thetimezone{+00:00}%
  }%
  {%
    \parsepdftimezone#7%
  }%
}

\def\parsepdftimezone#1'#2'{%
  \def\thetimezone{#1:#2}%
}

\expandafter\parsepdfdatetime\pdfcreationdate\endparsepdfdatetime
\let\pdfhour\thehour
\let\pdfminute\theminute
\let\pdfsecond\thesecond
\let\pdftimezone\thetimezone

\newcommand*{\pdfnowtime}{\timefmt{\pdfhour}{\pdfminute}{\pdfsecond}{\pdftimezone}}

\newcommand*{\pdfnow}{%
 \number\year-\two@digits{\month}-\two@digits{\day}
 \pdfnowtime
}

\newcommand*{\filedate}[1]{%
  \expandafter\parsepdfdatetime\pdffilemoddate{#1}\endparsepdfdatetime
  \theyear-\two@digits{\themonth}-\two@digits{\theday}\  
  \timefmt{\thehour}{\theminute}{\thesecond}{\thetimezone}%
}

% Define an ordinal command:
\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
}

% Define registers needed by \printdate:
\newcount\julianday
\newcount\dayofweek

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

% Define command to convert ISO date to a form that can be used by \datefmt:
\newcommand*{\printdate}[1]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \pgfcalendarjuliantodate{\julianday}{\thisyear}{\thismonth}{\thisday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  % Now display the date:
  \datefmt[\dayofweek]{\thisyear}{\thismonth}{\thisday}%
}

% Redefine \today to use the same format:
\renewcommand*{\today}{%
  \pgfcalendardatetojulian{\year-\month-\day}{\julianday}%
  \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
  \datefmt[\dayofweek]{\year}{\month}{\day}%
}

% Check if babel is used with the british option:
\AtBeginDocument{%
  \ifdef{\datebritish}% check if \datebritish exists
  {% it does exist, so redefine it
    \renewcommand*{\datebritish}{%
      \renewcommand*{\today}{%
        \pgfcalendardatetojulian{\year-\month-\day}{\julianday}%
        \pgfcalendarjuliantoweekday{\julianday}{\dayofweek}%
        \datefmt[\dayofweek]{\year}{\month}{\day}%
      }%
    }%
  }%
  {}% doesn't exist, do nothing
}

\endinput
And here's the code for the sample document:
\documentclass{article}

\usepackage{mycustomdatetime}

\begin{document}

The file \jobname.tex was last modified on:
\filedate{\jobname.tex}.

The PDF was built by \TeX\ on: \pdfnow.

Format a specific time (Zulu time):
\timefmt{8}{10}{35}{+0:00}.

Format a specific time (non-Zulu time):
\timefmt{8}{10}{35}{+1:00} or
\timefmt{8}{10}{35}{-4:30} or
\timefmt{8}{10}{35}{+5:45}.

Format a specific time without a time zone:
\timefmt{8}{10}{35}{}.

Format a specific time with a time zone but without seconds:
\timefmt{8}{10}{}{+00:00}.

Format a specific time without a time zone or seconds:
\timefmt{8}{10}{}{}.

\end{document}

Download customdatetime.tex or customdatetime.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