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

如何在XSLT中仅移除时间戳末尾的秒部分(:00)

解决XSLT中移除时间戳秒部分的问题

你当前代码的问题在于:先把时间里的所有冒号去掉得到150000,再替换00时会把分钟的0和秒的0一起删掉,所以最终得到15。下面给你几种实用的解决方法:

方法1:截取时分部分再去冒号

直接提取时间字符串的前5位(也就是HH:MM格式的时分),再去掉冒号:

<xsl:variable name="time-part" select="substring-after($date-time, ' ')" as="xs:string"/>
<xsl:variable name="time" select="replace(substring($time-part, 1, 5), ':', '')" as="xs:string"/>

比如处理15:00:00时,先拿到15:00,去掉冒号后就是1500。

方法2:用tokenize拆分后拼接时分

把时间按冒号拆成小时、分钟、秒三个片段,直接取前两个拼接:

<xsl:variable name="time-tokens" select="tokenize(substring-after($date-time, ' '), ':')" as="xs:string*"/>
<xsl:variable name="time" select="concat($time-tokens[1], $time-tokens[2])" as="xs:string"/>

这种方法更灵活,哪怕秒不是00,也能正确提取时分部分拼接,比如14:35:45会变成1435。

方法3:精准替换末尾的秒部分

用正则匹配字符串末尾的:00,替换为空后再去掉剩余的冒号:

<xsl:variable name="time" select="replace(replace(substring-after($date-time, ' '), ':00$', ''), ':', '')" as="xs:string"/>

这里的$表示匹配字符串结尾,只会替换最后面的:00,不会影响前面的分钟部分,处理后得到1500。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 18:53:16