gpt4 book ai didi

javascript - 更改模板上的日期格式

转载 作者:行者123 更新时间:2023-12-03 04:24:07 24 4
gpt4 key购买 nike

我正在使用Foundry template在 Squarespace 上,我需要将帖子页面上的日期格式从英语更改为葡萄牙语。我需要的不是“May 6”,而是“6 Mai”。在巴西,我们使用 dd/mm/yyyy 模式。在本例中,我只需要日期和月份,并翻译所有月份(翻译为:Jan、Fev、Mar、Abr、Mai、Jun、Jul、Ago、Set、Out、Nov、Dez)。

我已经看到人们在那里为其他语言解决了这个问题。但不是葡萄牙语或 Foundry 模板。可以在 Squarespace 的页眉或页脚上进行代码注入(inject)。我只需要一个可以做到这一点的 JavaScript,覆盖主题的默认日期格式。

最佳答案

我将通过以下 Javascript 来处理它,通过代码注入(inject)插入。请注意,虽然某些月份缩写是相同的,但为了清晰起见,我将它们包括在内,以便其他人可以更方便地重复使用。另外,我用于键的缩写(即原始月份缩写)可能不是 Squarespace 实际使用的,因此它们可能需要更新。

<script>
(function() {
var dates = document.getElementsByClassName("dt-published date-highlight");
var newDate;
var i,I;
// Create object with 'source' keys on the left, and 'output' values on the right.
var months = {
"Jan":"Jan",
"Feb":"Fev",
"Mar":"Mar",
"Apr":"Abr",
"May":"Mai",
"Jun":"Jun",
"Jul":"Jul",
"Aug":"Ago",
"Sep":"Set",
"Oct":"Out",
"Nov":"Nov",
"Dec":"Dez"
};
// Loop through all dates, replacing months and reordering display.
// - Trim extra white space from beginning and end of date.
// - Replace multiple consecutive spaces with a single space.
// - Split by space into an array.
// - Replace month text based on 'months' object key:value pairs.
// - Convert array to string, rearranging display order of elements.
// - Set new date HTML.
for (i=0, I=dates.length; i<I; i++) {
newDate = dates[i].innerHTML.trim();
newDate = newDate = newDate.replace(/ +/g, ' ');
newDate = newDate.split(" ");
newDate[0] = months[newDate[0]];
newDate = newDate[1] + " " + newDate[0];
dates[i].innerHTML = newDate;
}
})();
</script>

关于javascript - 更改模板上的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com