XSLT:两个日期之间的年份差异
我想计算 XSLT 2.0 中两个日期之间的年份差异。我尝试过:
xs:dayTimeDuration(xs:date('2007-03-02')-xs:date('2002-03-01'))
给我天数的差异:
P1827D
有一个函数可以计算两个日期之间的年、月和天的某种“标准化”xs:duration
,就像本例中的 P1Y0M1D
一样,我可以从中提取年份 P1Y
。如上所述,从仅由天组成的 xs:duration
中提取年份是不可能的(也没有多大意义)。
那么如何计算两个日期的相差年数而不是天数呢?
I would like to compute the year difference between two dates in XSLT 2.0. I tried:
xs:dayTimeDuration(xs:date('2007-03-02')-xs:date('2002-03-01'))
giving me the difference in days:
P1827D
What would help would be a function that calculates a somehow "normalized" xs:duration
in years, months and days between two dates, like P1Y0M1D
in this example, which I can extract the year P1Y
from. Extracting a year from an xs:duration
consisting of days only, as above, is not possible (and wouldn't make much sense either).
So how do I compute the difference of two dates in years, not in days?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜(!)您想要返回一个相当于以整年表示的年龄的结果,使用习惯规则,即年龄在每个生日时增加一。这可以使用以下模板(您可以将其转换为函数)来计算:
请注意,这假设 2 月 29 日出生的人的生日发生在非闰年的 3 月 1 日。
I am guessing (!) you want to return a result that is equivalent to age expressed in whole years, using the customary rule where the age increases by one on each birthday. This could be calculated using the following template (which you could turn into a function):
Do note that this assumes the birthday of a person born on February 29 occurs on March 1 in a non-leap year.
我自己想出了一个递归解决方案。它不是性能最高的,但它可以工作:
您可以使用以下代码验证该功能:
打印出:
I figured out a recursive solution by myself. It is not the most performant, but it works:
You can verify the function using the following code:
which prints out: