更改模板上的日期格式 [英] Change date format on a template

查看:248
本文介绍了更改模板上的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Squarespace上使用 Foundry模板,我需要更改日期格式从英文到葡萄牙文的页面。而不是五六,我需要六迈。在巴西,我们使用模式dd / mm / yyyy。在这种情况下,我只想要一天一个月,还要翻译所有的月份(到:Jan,Fev,Mar,Abr,Mai,Jun,Jul,Ago,Set,Out,Nov,Dez)。 b
$ b

我已经看到人们正在为其他语言解决这个问题。但不要葡萄牙或在铸造模板。在Squarespace的头部或页脚上进行代码注入是可能的。我只需要一个可以做到这一点的Javascript,覆盖这个主题的默认日期格式。 会通过下面的JavaScript通过代码注入插入它。请注意,尽管一些月份的缩写是相同的,为了清楚起见,我已经将它们包括在内,以便其他人可以更加重用。此外,我用于键的缩写(也就是原来的月份缩写)可能不是Squarespace实际使用的缩写,因此可能需要更新。

 <脚本> 
(function(){
var dates = document.getElementsByClassName(dt-published date-highlight);
var newDate;
var i,I;
//在左边创建源键的对象,在右边创建输出值。
var month = {
Jan:Jan,
Feb: Fev,
Mar:Mar,
Apr:Abr,
May:Mai,
Jun: ,
Jul:Jul,
Aug:Ago,
Sep:Set,
Oct:Out
Nov:Nov,
Dec:Dez
};
//循环所有日期,替换月份并重新排序显示
// - 从日期的开始和结尾修剪额外的空白
// - 用一个空格替换多个连续的空格
// - 空格拆分成数组
// - 根据星期一取代月份文本对象键:值对。
// - 将数组转换为字符串,重新排列元素的显示顺序。
// - 设置新的日期HTML。
for(i = 0,I = dates.length; 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>


I'm using the Foundry template on Squarespace and I need to change the date format on post pages from english to portuguese. Instead of "May 6" I need "6 Mai". In Brazil we use the pattern 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).

I already saw people solving this for others languages there. But not to portuguese or on the Foundry template. It's possible to make a code-injection on Squarespace, on the head or footer. I just need a Javascript that can do that, overwriting the theme's default date format.

解决方案

I would approach it via the following Javascript, inserted via code injection. 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.

<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>

这篇关于更改模板上的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆