简体   繁体   English

更改模板上的日期格式

[英]Change date format on a template

I'm using the Foundry template on Squarespace and I need to change the date format on post pages from english to portuguese. 我在Squarespace上使用Foundry模板 ,我需要将帖子页面上的日期格式从英语更改为葡萄牙语。 Instead of "May 6" I need "6 Mai". 而不是“ 5月6日”,我需要“ 6 Mai”。 In Brazil we use the pattern dd/mm/yyyy. 在巴西,我们使用dd / mm / yyyy模式。 In this case I just want the day and month, and also translate all the months (to: Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez). 在这种情况下,我只需要日期和月份,还可以翻译所有月份(至:Jan,Fev,Mar,Abr,Mai,Jun,Jul,Ago,Set,Out,Nov,Dez)。

I already saw people solving this for others languages there. 我已经看到人们在那里解决其他语言的问题。 But not to portuguese or on the Foundry template. 但不要葡萄牙语或使用Foundry模板。 It's possible to make a code-injection on Squarespace, on the head or footer. 可以在Squarespace的页眉或页脚上进行代码注入。 I just need a Javascript that can do that, overwriting the theme's default date format. 我只需要一个可以做到的Javascript,就可以覆盖主题的默认日期格式。

I would approach it via the following Javascript, inserted via code injection. 我将通过以下通过代码注入插入的Javascript处理它。 Note that although some of the month abbreviations are the same, I've included them for clarity and so that it may be more reusable for others. 请注意,尽管月份中的某些缩写是相同的,但为了清楚起见,我将它们包括在内,以便对其他缩写可能更可重用。 Also, the abbreviations I've used for the keys (that is, the original month abbreviations) may not be what Squarespace actually uses, so they may need to be updated. 另外,我为键使用的缩写(即原始月份的缩写)可能不是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM