【问题标题】:Change date format on a template更改模板上的日期格式
【发布时间】:2017-10-04 17:53:50
【问题描述】:

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

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

【问题讨论】:

    标签: javascript date format overwrite squarespace


    【解决方案1】:

    我会通过以下 Javascript 来处理它,通过代码注入插入。请注意,尽管有些月份的缩写是相同的,但为了清楚起见,我将它们包括在内,以便其他人可以更容易地重复使用它们。此外,我用于键的缩写(即原始月份缩写)可能不是 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>
    

    【讨论】:

    • 嗨,布兰登,代码运行良好。感谢您的帮助和澄清!您刚刚在结束
    猜你喜欢
    • 2019-05-29
    • 2014-10-25
    • 2018-10-15
    • 2020-11-26
    • 2011-12-05
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多