I want to know if there is a way to get the date elements (year, month, day, hour, minute, second) in order to create a lucky of version number that automatically increments. For example if now it is 15:30 hs of March 19 of the year 2018, I would like to generate a number like: 201803191530 where the format is YYYYMMDDHHmm. I would like to have the possibility to arrange the elements in any way. It would be ideal some sort of comands like \year \month \day \hour \minute \second
, for example, so then I can use them as I wish.
An expandable time stamp definition without any package:
\documentclass{article}
\makeatletter
\edef\timestamp{%
\the\year
\two@digits\month
\two@digits\day
\two@digits{\numexpr(\time*2 - 59)/120\relax}%
\two@digits{\numexpr\time - 60*\numexpr(\time*2 - 59)/120\relax\relax}%
}
\makeatother
\typeout{Time stamp: \timestamp}
\begin{document}
Time stamp: \timestamp
\end{document}
Remarks:
\time
gets the minutes of the day.- e-TeX's
\numexpr
allows expandable calculations. However, it rounds. The complicate formulas work around it. \two@digits
comes from the LaTeX kernel and is intended for positive integers. If the number is less than 10, it adds a preceding zero.
If you use pdflatex
, you can use \pdfcreationdate
that records the time stamp when the PDF file is opened for output, which is a string of the form
D:20170421011556+02’00’
The last seven characters represent the difference with UTC. So it's just a matter of parsing the string. I added a possible definition for your time stamp, reorder the items at will.
\documentclass{article}
\makeatletter
\begingroup
\def\set@time@data#1:#2\@nil{\set@time@data@a#2}
\def\set@time@data@a#1#2#3#4{%
\gdef\Year{#1#2#3#4}%
\set@time@data@b
}
\def\set@time@data@b#1#2#3#4{%
\gdef\Month{#1#2}%
\gdef\Day{#3#4}%
\set@time@data@c
}
\def\set@time@data@c#1#2#3#4#5#6{%
\gdef\Hour{#1#2}%
\gdef\Min{#3#4}%
\gdef\Sec{#5#6}%
\set@time@data@d
}
\def\set@time@data@d#1#2#3#4#5#6#7{}
% start up the business
\expandafter\set@time@data\pdfcreationdate\@nil
\endgroup
\edef\TimeStamp{\Year-\Month-\Day\space\Hour:\Min:\Sec}
\makeatother
\begin{document}
Time stamp: \TimeStamp
\medskip
\begin{tabular}{@{}ll}
Year & \Year \\
Month & \Month \\
Day & \Day \\
Hour & \Hour \\
Min & \Min \\
Sec & \Sec \\
\end{tabular}
\end{document}
Similar, but with clearer syntax (here I just define the \TimeStamp
macro); just move around the various ranges.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_set:Npx \TimeStamp
{
\str_range:Nnn \pdfcreationdate { 3 } { 6 } % year
-
\str_range:Nnn \pdfcreationdate { 7 } { 8 } % month
-
\str_range:Nnn \pdfcreationdate { 9 } { 10 } % day
\space
\str_range:Nnn \pdfcreationdate { 11 } { 12 } % hour
:
\str_range:Nnn \pdfcreationdate { 13 } { 14 } % minutes
:
\str_range:Nnn \pdfcreationdate { 15 } { 16 } % seconds
}
\ExplSyntaxOff
\begin{document}
Time stamp: \TimeStamp
\end{document}
datetime2
package? It allows you to access the date and time with customized formatting. – Marcel Krüger Mar 19 '18 at 18:59