22

I'm using the day of the year a lot in my documents and I wonder if I can have LaTex calculating it for me? Example: 2013178 where 2013 is (surprise) the current year (which I know could be achieved by \the\year) and 178, the day of the year.

Was googling both in english and in german ("LaTex day of the year" and "Latex Tag des Jahres") and found only posts and howtos regarding the day of the week and month.

PS: I tagged it with macros since I assume that this might need one to be solved.

1
  • try googling \today command and it's features.
    – Crowley
    Jun 27, 2013 at 13:32

4 Answers 4

4

Why not the simpler way of datenumber?

MWE

\documentclass{article}
\usepackage{xcolor}         % to distinguish coupled numbers 
\usepackage{datenumber}
\setdate{2013}{6}{27}       % date of the OP
\setstartyear{\thedateyear} % count days from here


\begin{document}

The \datedate{} was  \thedateyear{\color{red!80!black}\thedatenumber} \\

Today (\today) \setdatetoday is \thedateyear{\color{blue}\thedatenumber}

\end{document}
2
  • This indeed looks a lot simpler. I'll give it a try. From my perspective (not a day to day LaTex user) I should mark it as accepted if it's working, shouldn't I?
    – aaki
    Jul 4, 2013 at 6:21
  • Works. Marked as accepted since it is the simplest but working answer from a newbie point of view. Anyways, thanks to all for those terrific answers. Never would have expected so much answers.
    – aaki
    Jul 4, 2013 at 7:46
22

You can use PGF's calendar library to convert the current day and the first day of the current year into julian dates:

\documentclass{article}

\usepackage{pgfkeys, pgfcalendar}

\newcount\julianA
\newcount\julianB

\newcommand\doy{%
    \pgfcalendardatetojulian{\year-\month-\day}{\julianA}%
    \pgfcalendardatetojulian{\year-1-1}{\julianB}%
    \advance\julianA by -\julianB%
    \advance\julianA by 1\relax%
    \the\year\the\julianA%
}

\begin{document}
Day of the year: \doy
\end{document}
1
  • 1
    Thanks, this was exactly what I was looking for! It's almost too perfect, I just needed to copy and paste it... :)
    – aaki
    Jun 27, 2013 at 13:57
16

My attempt with the powerful expl3. I know the interface is not good at all, but please bear with me, I'm stupid a newbie. :)

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\bool_new:N \l_leap_year_bool
\int_new:N \l_day_of_year_int
\cs_generate_variant:Nn \int_to_arabic:n { V }

\DeclareDocumentCommand \dayoftheyear { m m m }
{
  \bool_set:Nn \l_leap_year_bool
  {
    \int_compare_p:n { \int_mod:nn { #3 } { 4 } = 0 } &&
    \int_compare_p:n { \int_mod:nn { #3  } { 100 } != 0 } ||
    \int_compare_p:n { \int_mod:nn { #3  } { 400 } = 0 }
  }
  \int_case:nnn { #2 - 1 }
  {
    { 1 } { \int_set:Nn \l_day_of_year_int { 31 } }
    { 2 } { \int_set:Nn \l_day_of_year_int { 59 } }
    { 3 } { \int_set:Nn \l_day_of_year_int { 90 } }
    { 4 } { \int_set:Nn \l_day_of_year_int { 120 } }
    { 5 } { \int_set:Nn \l_day_of_year_int { 151 } }
    { 6 } { \int_set:Nn \l_day_of_year_int { 181 } }
    { 7 } { \int_set:Nn \l_day_of_year_int { 212 } }
    { 8 } { \int_set:Nn \l_day_of_year_int { 243 } }
    { 9 } { \int_set:Nn \l_day_of_year_int { 273 } }
    { 10 } { \int_set:Nn \l_day_of_year_int { 304 } }
    { 11 } { \int_set:Nn \l_day_of_year_int { 334 } }
  }
  { }
  \bool_if:nT 
  {
    \l_leap_year_bool && \int_compare_p:n { #2 > 2}
  } { \int_incr:N \l_day_of_year_int }
  \int_add:Nn \l_day_of_year_int { #1 }
  \int_to_arabic:V \l_day_of_year_int
}
\ExplSyntaxOff

\begin{document}

Day of the year: \dayoftheyear{27}{6}{2013}

\end{document}

Hope it helps. :)

9
  • 2
    Very nice! :) Some remarks, though: you should put the \bool_new:N and \int_new:N assignments outside of the definition of \dayoftheyear (or else call it twice and be surprised ;) ). Also instead of \bool_if:nT { \leap_year_bool } you can say \bool_if:NT \leap_year_bool. A last one: variables should start with \l_ or \g_ denoting if they're local or global.
    – cgnieder
    Jun 27, 2013 at 14:50
  • @cgnieder: Thanks Clemens! I always keep forgetting about the good practices. :) I'll fix them. Jun 27, 2013 at 14:50
  • @cgnieder: hopefully fixed now. :) That explains why I saw an old L3 code of mine and thought, "Why did I put these \l_ prefixes?" :) Jun 27, 2013 at 14:54
  • much better :) Maybe \int_to_arabic:n \l_day_of_year_int should also follow the l3 conventions. As an n type argument put in braces \int_to_arabic:n { \l_day_of_year_int } or - maybe even nicer - generate a V type variant (\cs_generate_variant:Nn \int_to_arabic:n { V }) and use \int_to_arabic:V \l_day_of_year_int.
    – cgnieder
    Jun 27, 2013 at 16:33
  • @cgnieder: ooh that's pure gold, I updated the code accordingly. Danke! :) Jun 28, 2013 at 10:59
14

I knew that one day my \juliandate_calc:nnnn macro would have been useful. ;-)

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\dayofyear}{o}
 {
  \IfNoValueTF{#1}
   {
    \aaki_day_of_year:nnn { \year } { \month } { \day }
   }
   {
    \aaki_day_of_year:n { #1 }
   }
 }
\cs_new:Npn \aaki_day_of_year:n #1
 {
  \aaki_day_of_year:w #1 \q_stop
 }
\cs_new:Npn \aaki_day_of_year:w #1 - #2 - #3 \q_stop
 {
  \aaki_day_of_year:nnn { #1 } { #2 } { #3 }
 }

\cs_new:Npn \aaki_day_of_year:nnn #1 #2 #3
 {
  \int_eval:n { #1 }
  \int_eval:n 
   {
    \juliandate_calc:nnnn { #3 } { #2 } { #1 } { \use:n }
    -
    \juliandate_calc:nnnn { 1 } { 1 } { #1 } { \use:n }
    + 1
   }
 }

\cs_new:Npn \juliandate_calc:nnnn #1 #2 #3 #4 % #1 = day, #2 = month, #3 = year, #4 = what to do
 {
  #4
   {
    \int_eval:n
     {
      #1 +
      \int_div_truncate:nn
       {
        153 * (#2 + 12 * \int_div_truncate:nn { 14 - #2 } { 12 } - 3) + 2
       }
       { 5 } +
      365 * (#3 + 4800 - \int_div_truncate:nn { 14 - #2 } { 12 } ) +
      \int_div_truncate:nn { #3 + 4800 - \int_div_truncate:nn { 14 - #2 } { 12 } } { 4 } -
      \int_div_truncate:nn { #3 + 4800 - \int_div_truncate:nn { 14 - #2 } { 12 } } { 100 } +
      \int_div_truncate:nn { #3 + 4800 - \int_div_truncate:nn { 14 - #2 } { 12 } } { 400 } -
      32045
     }
   }
 }

\ExplSyntaxOff

\begin{document}

\dayofyear

\dayofyear[2013-1-1]

\dayofyear[2013-12-31]

\dayofyear[2012-12-31]

\end{document}

The algorithm for getting the Julian date from day, month and year can be found on the net.

The internal command \aaki_day_of_year:n (for the date in ISO format YYYY-MM-DD) is fully expandable.

enter image description here

You must log in to answer this question.

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