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

如何调整JS代码将ItemDate格式从MM/dd/yyyy HH:mm:ss改为dd/MM/yyyy?

解决ItemDate日期格式转换问题

我来帮你搞定这个日期格式调整的需求!核心就是把原始的MM/dd/yyyy HH:mm:ss格式字符串,转换成dd/MM/yyyy的形式。这里给你两种实用的方案,你可以根据场景选择:

方案1:直接字符串拆分重组(简单高效)

这种方法适合你确定原始日期格式固定为MM/dd/yyyy HH:mm:ss的情况,不需要依赖Date对象,处理速度快:

首先在你的脚本里添加一个日期格式化函数:

function formatDate(dateStr) {
    // 先把日期和时间部分拆分,只保留日期部分
    const datePart = dateStr.split(' ')[0];
    // 拆分月、日、年
    const [month, day, year] = datePart.split('/');
    // 重新组合成dd/MM/yyyy格式
    return `${day}/${month}/${year}`;
}

然后修改repeatingSectionViewTemplate函数里的循环部分,把原本直接获取ItemDate文本的代码替换成调用这个函数:

$xml.find("Item").each(function() {
    // 替换这里的ItemDate处理逻辑
    const itemDate = formatDate($(this).find("ItemDate").text());
    htm = htm + "<tr><td width='50px'>" + $(this).find("ItemNumber").text() + "</td><td width='140px'>" + $(this).find("ItemDescription").text() + "</td><td width='70px'>" + $(this).find("ItemStatus").text() + "</td><td>" + itemDate +"</td><td>" + $(this).find("CollectedByUser").text() +"</td></tr>";
});

方案2:用Date对象处理(更健壮)

如果担心未来日期格式有微小变化(比如时分秒的分隔符不同),可以用Date对象来解析,兼容性更好:

添加的格式化函数改成这样:

function formatDate(dateStr) {
    // 解析原始日期字符串为Date对象
    const date = new Date(dateStr);
    // 提取日、月、年,用padStart保证是两位数(比如1号变成01)
    const day = String(date.getDate()).padStart(2, '0');
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 注意:Date对象的月份是0开始的,所以要加1
    const year = date.getFullYear();
    // 拼接成目标格式
    return `${day}/${month}/${year}`;
}

然后同样替换循环里的ItemDate处理部分即可。

修改后的完整代码

这里是整合了方案1的完整脚本,你可以直接替换原代码:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    Templates: {
        Fields: {
            'ItemsOverview': {
                'View': repeatingSectionViewTemplate
            }
        }
    }
});

// 新增的日期格式化函数
function formatDate(dateStr) {
    const datePart = dateStr.split(' ')[0];
    const [month, day, year] = datePart.split('/');
    return `${day}/${month}/${year}`;
}

function repeatingSectionViewTemplate(ctx) {
    var xml = ctx.CurrentItem["ItemsOverview"];
    var decodedxml = xml.DecodeXMLNotation();
    var htm = "";
    xmlDoc = $.parseXML( decodedxml );
    $xml = $( xmlDoc );
    $xml.find("Item").each(function() {
        const itemDate = formatDate($(this).find("ItemDate").text());
        htm = htm + "<tr><td width='50px'>" + $(this).find("ItemNumber").text() + "</td><td width='140px'>" + $(this).find("ItemDescription").text() + "</td><td width='70px'>" + $(this).find("ItemStatus").text() + "</td><td>" + itemDate +"</td><td>" + $(this).find("CollectedByUser").text() +"</td></tr>";
    });
    return "<table border='1px' width='550px' style='border-collapse:collapse;'><tr><th align='left' width='50px'>Item</th><th align='left' width='180px'>Description</th><th align='left' width='70px'>Status</th><th align='left' width='70px'>Date</th><th align='left' width='170px'>Collected By</th></tr>" + htm +"</table>";
};

//Replaces html notation to their equivalent xml escape characters.
String.prototype.DecodeXMLNotation = function () {
    var output = this;
    if ($.trim(output) != "") {
        output = output.replace(/&amp;apos;/g, "'").replace(/&amp;quot;/g, '"').replace(/&amp;gt;/g, '&gt;').replace(/&amp;lt;/g, '&lt;').replace(/&amp;amp;/g, '&amp;');
    } else {
        output = "";
    }
    return output;
};
</script>

这样修改后,你的ItemDate就会以dd/MM/yyyy的格式显示啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:06:16