如何根据自定义日期计算未来日期(例如 2018-05-05 + 14 天)?

如何根据自定义日期计算未来日期(例如 2018-05-05 + 14 天)?

我想为客户创建发票scrlttr2。在此我通过设置文档日期\setkomavar{date}{\today}

我重复使用这个日期日期自动将付款截止日期添加到信尾(截止日期 =今天的日期+未来 14 天)。

\today如果仅使用文档日期(参见 MWE),则这完全可以正常工作。


最小工作示例(MWE):

\documentclass{scrlttr2}
\usepackage{advdate}

\setkomavar{date}{\today}

\begin{document}
Today is \today. Please pay until \AdvanceDate[14]\today.
\end{document}

但是,我还想使用自定义日期,例如 2018-05-05 作为单据日期。您知道如何根据任意日期而不是\today仅根据日期生成上述付款期限吗?

在最好的情况下,我可以将\today以及插入2018-05-05到-命令中\setkomavar{date},因此以下示例应该将 14 天添加到我的自定义日期(使用自定义日期时)以及\today日期(如果用作\today文档日期):

\documentclass{scrlttr2}
\usepackage{advdate}

\setkomavar{date}{2018-05-05}

\begin{document}
Today is \today. Please pay until \AdvanceDate[14]\date.
\end{document}

也许有用的软件包是:

答案1

首先,\today和变量的内容是有区别的date。设置变量date不会改变\today。请看以下示例:

\documentclass{scrlttr2}
\begin{document}
% default value of date variable is \today
Today is \today{} and the variable date stores \usekomavar{date}.

% changing value for variable date does not change \today!
\setkomavar{date}{2018-05-05}
Today is \today{} and the date variable stores \usekomavar{date}.
\end{document}

结果

在此处输入图片描述


该包advdate提供了附加命令\SaveDate\SetDate\DayAfter(参见包文档),可用于获取所需的结果。

首先,我们advdate在宏中将今天的日期保存为 dd/mm/yyyy(的所有宏都使用此语法)\invoicedate

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

\invoicedate然后将用于自动设置 KOMA-Script 变量date和新变量paymentdate。您不必手动设置或更改这些变量!

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}

只有当你想为变量date(和依赖变量paymentdate)设置一个固定值时,你才需要使用以下宏\setinvoicedate。请注意,此宏会更改\invoicedate 设置 KOMA-Scripts 变量datepaymentdate自动执行。因此您只需将此宏用于不同的发票/信函日期。

\newcommand*\setinvoicedate[1]{%
  \ifstr{#1}{today}
    {\SaveDate[\invoicedate]}%
    {\renewcommand*\invoicedate{#1}}%
}

语法为\setinvoicedate{dd/mm/yyyy}。例如:

\setinvoicedate{24/12/2018}

您可以\setinvoicedate在一份文件中多次使用,例如,如果您的文件中有多张发票。如果您想将发票/信件日期重置为今天的日期,您可以使用

\setinvoicedate{today}

完整示例:

\documentclass{scrlttr2}
\usepackage{advdate}

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
  \ifstr{#1}{today}
    {\SaveDate[\invoicedate]}%
    {\renewcommand*\invoicedate{#1}}%
}
% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{24/12/2018}

\newcommand\demo{% only for this example
\begin{itemize}
  \item The invoice date is \usekomavar{date}.
  \item Please pay until \usekomavar{paymentdate}.
  \item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{11/03/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}

在此处输入图片描述

或者使用课程选项numericaldate

在此处输入图片描述

或者用包isodate来格式化日期输出:

在此处输入图片描述

代码:

\documentclass[english]{scrlttr2}
\usepackage{isodate}
\isodate
\usepackage{advdate}

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
  \ifstr{#1}{today}
    {\SaveDate[\invoicedate]}%
    {\renewcommand*\invoicedate{#1}}%
}

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}

% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{24/12/2018}

\newcommand\demo{% only for this example
\begin{itemize}
  \item The invoice date is \usekomavar{date}.
  \item Please pay until \usekomavar{paymentdate}.
  \item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{11/03/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}

更新关于以下评论:

为了避免周六或周日的付款日期,您可以加载包裹scrdate并使用

\setkomavar{paymentdate}{{%
  \SetDate[\invoicedate]%
  \ifnum\todaysnumber=6\DayAfter[16]% Saturday -> Monday
    \else\ifnum\todaysnumber=0\DayAfter[15]% Sunday -> Monday
      \else\DayAfter[14]
  \fi\fi
}}

例子:

\documentclass{scrlttr2}
\usepackage{scrdate}% <- added
\usepackage{advdate}

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{% <- changed
  \SetDate[\invoicedate]%
  \ifnum\todaysnumber=6\DayAfter[16]% Saturday -> Monday
    \else\ifnum\todaysnumber=0\DayAfter[15]% Sunday -> Monday
      \else\DayAfter[14]
  \fi\fi
}}

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
  \ifstr{#1}{today}
    {\SaveDate[\invoicedate]}%
    {\renewcommand*\invoicedate{#1}}%
}
% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{11/08/2018}

\newcommand\demo{% only for this example
\begin{itemize}
  \item The invoice date is \usekomavar{date}.
  \item Please pay until \usekomavar{paymentdate}.
  \item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{12/08/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{13/08/2018}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}

在此处输入图片描述

答案2

datetime2这是一个基于的简单版本这个答案。请注意,使用 KOMA 变量日期并不像看起来那么容易,我为此引入了一个单独的命令(它被\edef编辑为允许\today作为值)。这将用于设置 KOMA 变量并进行计算。

日期时间计算

\documentclass{scrlttr2}
% from https://tex.stackexchange.com/a/237129
\usepackage[calc,useregional]{datetime2}
\newcount\myct
\newcount\datecount
\newcommand{\myday}[2][0]{%
    \DTMsavedate{mydate}{#2}%
    \DTMsaveddateoffsettojulianday{mydate}{#1}{\myct}%
    \DTMsavejulianday{mydate}{\number\myct}%
    \DTMusedate{mydate}%
}


\edef\tmpdate{2018-05-05}
\setkomavar{date}{\tmpdate}

\begin{document}
Today is \today. Please pay until \myday[14]{2018-05-05}. This is still \today. \myday[14]{\tmpdate}
\end{document}

相关内容