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

标签 javascript date format overwrite squarespace

我正在使用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/

相关文章:

php - mysql php数组转特定格式

Java String.format 两位小数,以点作为千位分隔符,以逗号作为小数分隔符

javascript - 输入元素在 React 中未更新

javascript - 我们如何通过将 HTML 元素拖放到另一列中来克隆它(仅在该列中而不是在其外部)

javascript - 如何使 JQuery UI 布局插件在 Bootstrap body-content div 中工作

ios - 日期格式在 IOS 中不起作用

java - 日期解析魔法

r - 在 R 中导入 excel 文件时日期发生变化

javascript - 从 child 的数量/类型/ Prop 中提取父组件配置

c# - 如何将小数值显示为小数点后两位?