You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XSLT中编写代码获取比输入日期晚2天的日期

XSLT实现日期顺延2天方案

以下实现分为两个版本,可根据你使用的XSLT环境选择:

XSLT 2.0/3.0 实现(推荐)

这个版本原生支持日期类型运算,代码最简洁:

  • 先将输入的DD-MM-YYYY格式字符串转换为ISO标准日期格式YYYY-MM-DD
  • 调用内置日期运算功能加2天
  • 再把结果转换回DD-MM-YYYY格式输出

示例代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- 输入参数:用户输入的DD-MM-YYYY格式日期 -->
    <xsl:param name="inputDate" select="'24-09-2021'"/>

    <xsl:template match="/">
        <xsl:variable name="day" select="substring($inputDate, 1, 2)"/>
        <xsl:variable name="month" select="substring($inputDate, 4, 2)"/>
        <xsl:variable name="year" select="substring($inputDate, 7, 4)"/>
        <!-- 转成ISO日期后加2天 -->
        <xsl:variable name="isoDate" select="xs:date(concat($year, '-', $month, '-', $day)) + xs:dayTimeDuration('P2D')"/>
        <!-- 转回DD-MM-YYYY格式输出 -->
        <xsl:value-of select="concat(format-date($isoDate, '[D01]'), '-', format-date($isoDate, '[M01]'), '-', format-date($isoDate, '[Y0001]'))"/>
    </xsl:template>
</xsl:stylesheet>

测试输入24-09-2021时,输出结果为26-09-2021,符合要求。

XSLT 1.0 实现(兼容旧环境)

XSLT 1.0没有内置日期运算函数,需要手动实现日期拆分、天数累加、月份/年份进位逻辑,还要处理闰年和各月天数差异:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="inputDate" select="'24-09-2021'"/>

    <!-- 计算目标日期的模板 -->
    <xsl:template name="addTwoDays">
        <xsl:param name="d"/>
        <xsl:param name="m"/>
        <xsl:param name="y"/>
        <!-- 各月天数,二月单独判断闰年 -->
        <xsl:variable name="monthDays">
            <xsl:choose>
                <xsl:when test="$m=2">
                    <xsl:choose>
                        <xsl:when test="($y mod 4 = 0 and $y mod 100 != 0) or $y mod 400 = 0">29</xsl:when>
                        <xsl:otherwise>28</xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:when test="$m=4 or $m=6 or $m=9 or $m=11">30</xsl:when>
                <xsl:otherwise>31</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <!-- 计算新的天、月、年 -->
        <xsl:variable name="newD" select="$d + 2"/>
        <xsl:choose>
            <xsl:when test="$newD <= $monthDays">
                <xsl:value-of select="concat(format-number($newD, '00'), '-', format-number($m, '00'), '-', $y)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="remainingD" select="$newD - $monthDays"/>
                <xsl:variable name="newM" select="$m + 1"/>
                <xsl:choose>
                    <xsl:when test="$newM <= 12">
                        <xsl:value-of select="concat(format-number($remainingD, '00'), '-', format-number($newM, '00'), '-', $y)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(format-number($remainingD, '00'), '-01-', $y + 1)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="/">
        <xsl:variable name="day" select="number(substring($inputDate, 1, 2))"/>
        <xsl:variable name="month" select="number(substring($inputDate, 4, 2))"/>
        <xsl:variable name="year" select="number(substring($inputDate, 7, 4))"/>
        <xsl:call-template name="addTwoDays">
            <xsl:with-param name="d" select="$day"/>
            <xsl:with-param name="m" select="$month"/>
            <xsl:with-param name="y" select="$year"/>
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

该实现支持边界场景运算:比如输入31-12-2023输出02-01-2024,输入28-02-2024输出01-03-2024。

内容的提问来源于stack exchange,提问作者sameena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:06:03