使用日期进行计算

使用日期进行计算

我想制作一个表格,在其中描述每周两个特定日期的一些事实。这些日期在表格中是固定的,但它们是任意选择的。我想自动打印每个日期的日期。

在下一个 MWE 中几乎完成了所有工作,但它只适用于\today,我不知道如何改变开始日期,类似于\setstartdate{}

此代码涉及一些日期计算,因此我从中摘取了一些代码

插入相对日期和时间规范?

但该算法存在一些问题。

我该如何修复该问题?或者如何使用其他代码来改进它?

这是 MWE

\documentclass{article}
\usepackage{xstring,datenumber,advdate,datetime,pgf,calc}
\usepackage{longtable}

\newcounter{dateOffset}%
\newcommand*{\SetDateOffsetForNext}[1]{%
\pgfmathsetcounter{dateOffset}{7-int(\the\value{datedayname})}% Initialize
\IfStrEqCase{#1}{%
    {mon}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+1,7))}}%
    {tue}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+2,7))}}%
    {wed}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+3,7))}}%
    {thu}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+4,7))}}%
    {fri}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+5,7))}}%
    {sat}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+6,7))}}%
    {sun}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+7,7))}}%
    }[\PackageError{\SetDateOffsetForNext}{Do not know "#1" as day of week}{}]%
    \IfEq{\the\value{dateOffset}}{0}{\pgfmathsetcounter{dateOffset}{0}}{}%
}%

\newcommand{\dayone}{mon}
\newcommand{\daytwo}{wed}

\newcounter{weeknumber}
\newcounter{sessionnumber}
\newcommand{\rowdayone}[1]{\addtocounter{weeknumber}{1}\addtocounter{sessionnumber}{1}%
\theweeknumber{} -- \thesessionnumber &
\SetDateOffsetForNext{\dayone}% Determine date offset
\addtocounter{dateOffset}{7*(\theweeknumber-1)}%
\AdvanceDate[\thedateOffset]% Advance to specified day
\today% Print specified date
%\AdvanceDate[-\thedateOffset]% Restore current day
& #1}
\newcommand{\rowdaytwo}[1]{\addtocounter{sessionnumber}{1}%
\theweeknumber{} -- \thesessionnumber &
\SetDateOffsetForNext{\daytwo}% Determine date offset
\addtocounter{dateOffset}{7*(\theweeknumber-1)}%
\AdvanceDate[\thedateOffset]% Advance to specified day
\today% Print specified date
%\AdvanceDate[-\thedateOffset]% Restore current day
& #1}

\newdateformat{short}{%
    \twodigit{\THEDAY}/\twodigit{\THEMONTH}%
    }


\begin{document}\short

%\SetDate[11/01/2015]

\begin{longtable}{|c|c|c|}
\rowdayone{description day 1 of week 1} \\
\rowdaytwo{description day 2 of week 1} \\
\rowdayone{description day 1 of week 2} \\
\rowdaytwo{description day 2 of week 2} \\
\rowdayone{description day 1 of week 3}
\end{longtable}

Last update \today

\end{document}

答案1

由于您同时使用datenumberadvdate套餐,因此您必须为这两个套餐设置日期。

例如,要指定对应于 1995 年 11 月 3 日的日期,您必须发出

\setdatenumber{1995}{11}{03}

对于datenumber包裹,以及

\SetDate[03/11/1995]

用于advdate包裹。

梅威瑟:

\documentclass{article}
\usepackage{xstring,datenumber,advdate,datetime,pgf,calc}
\usepackage{longtable}

\newcounter{dateOffset}%
\newcommand*{\SetDateOffsetForNext}[1]{%
\pgfmathsetcounter{dateOffset}{7-int(\the\value{datedayname})}% Initialize
\IfStrEqCase{#1}{%
    {mon}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+1,7))}}%
    {tue}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+2,7))}}%
    {wed}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+3,7))}}%
    {thu}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+4,7))}}%
    {fri}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+5,7))}}%
    {sat}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+6,7))}}%
    {sun}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+7,7))}}%
    }[\PackageError{\SetDateOffsetForNext}{Do not know "#1" as day of week}{}]%
    \IfEq{\the\value{dateOffset}}{0}{\pgfmathsetcounter{dateOffset}{0}}{}%
}%

\newcommand{\dayone}{mon}
\newcommand{\daytwo}{wed}

\newcounter{weeknumber}
\newcounter{sessionnumber}
\newcommand{\rowdayone}[1]{\addtocounter{weeknumber}{1}\addtocounter{sessionnumber}{1}%
\theweeknumber{} -- \thesessionnumber &
\SetDateOffsetForNext{\dayone}% Determine date offset
\addtocounter{dateOffset}{7*(\theweeknumber-1)}%
\AdvanceDate[\thedateOffset]% Advance to specified day
\today% Print specified date
%\AdvanceDate[-\thedateOffset]% Restore current day
& #1}
\newcommand{\rowdaytwo}[1]{\addtocounter{sessionnumber}{1}%
\theweeknumber{} -- \thesessionnumber &
\SetDateOffsetForNext{\daytwo}% Determine date offset
\addtocounter{dateOffset}{7*(\theweeknumber-1)}%
\AdvanceDate[\thedateOffset]% Advance to specified day
\today% Print specified date
%\AdvanceDate[-\thedateOffset]% Restore current day
& #1}

\newdateformat{short}{%
    \twodigit{\THEDAY}/\twodigit{\THEMONTH}%
    }


\begin{document}\short

\setdatenumber{1995}{11}{03}
\SetDate[03/11/1995]

\begin{longtable}{|c|c|c|}
\rowdayone{description day 1 of week 1} \\
\rowdaytwo{description day 2 of week 1} \\
\rowdayone{description day 1 of week 2} \\
\rowdaytwo{description day 2 of week 2} \\
\rowdayone{description day 1 of week 3}
\end{longtable}

Last update \today

\end{document} 

输出:

在此处输入图片描述

相关内容