Day of the year
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}
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. :)
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.