Moment.js display Chinese

Keywords: Unix IE github

Moment.js display Chinese

1, Reference article: moment.js add Chinese language (Chinese)

2, Start

0. Premise background

Recently, I was working on a project of wechat applet, in which time stamps are needed. Then I thought of using Moment.js to process the class library, wrote a piece of code, tested it, and the printed date is displayed in English. Considering the needs of users, it needs to be set to Chinese.

1. Find the Chinese dictionary section of Moment.js

Then I found the article on the Internet (reference article: moment.js add Chinese language (Chinese) )Look at the general steps,

moment.js provides support for multiple languages. First, find the Chinese dictionary on github:

moment.defineLocale('zh-cn', {  
        months : 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),  
        monthsShort : '1 month_2 month_3 month_4 month_5 month_6 month_7 month_8 month_9 month_10 month_11 month_12 month'.split('_'),  
        weekdays : 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),  
        weekdaysShort : 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),  
        weekdaysMin : 'day_One_Two_Three_Four_Five_Six'.split('_'),  
        longDateFormat : {  
            LT : 'Ah spot mm branch',  
            LTS : 'Ah spot m branch s second',  
            L : 'YYYY-MM-DD',  
            LL : 'YYYY year MMMD day',  
            LLL : 'YYYY year MMMD day Ah spot mm branch',  
            LLLL : 'YYYY year MMMD day ddddAh spot mm branch',  
            l : 'YYYY-MM-DD',  
            ll : 'YYYY year MMMD day',  
            lll : 'YYYY year MMMD day Ah spot mm branch',  
            llll : 'YYYY year MMMD day ddddAh spot mm branch'  
        },  
        meridiemParse: /Before dawn|Morning|morning|Noon|Afternoon|Night/,  
        meridiemHour: function (hour, meridiem) {  
            if (hour === 12) {  
                hour = 0;  
            }  
            if (meridiem === 'Before dawn' || meridiem === 'Morning' ||  
                    meridiem === 'morning') {  
                return hour;  
            } else if (meridiem === 'Afternoon' || meridiem === 'Night') {  
                return hour + 12;  
            } else {  
                // 'noon'  
                return hour >= 11 ? hour : hour + 12;  
            }  
        },  
        meridiem : function (hour, minute, isLower) {  
            var hm = hour * 100 + minute;  
            if (hm < 600) {  
                return 'Before dawn';  
            } else if (hm < 900) {  
                return 'Morning';  
            } else if (hm < 1130) {  
                return 'morning';  
            } else if (hm < 1230) {  
                return 'Noon';  
            } else if (hm < 1800) {  
                return 'Afternoon';  
            } else {  
                return 'Night';  
            }  
        },  
        calendar : {  
            sameDay : function () {  
                return this.minutes() === 0 ? '[Today]Ah[Dot ordering]' : '[Today]LT';  
            },  
            nextDay : function () {  
                return this.minutes() === 0 ? '[Tomorrow]Ah[Dot ordering]' : '[Tomorrow]LT';  
            },  
            lastDay : function () {  
                return this.minutes() === 0 ? '[Yesterday]Ah[Dot ordering]' : '[Yesterday]LT';  
            },  
            nextWeek : function () {  
                var startOfWeek, prefix;  
                startOfWeek = moment().startOf('week');  
                prefix = this.unix() - startOfWeek.unix() >= 7 * 24 * 3600 ? '[lower]' : '[book]';  
                return this.minutes() === 0 ? prefix + 'dddAh Dot ordering' : prefix + 'dddAh spot mm';  
            },  
            lastWeek : function () {  
                var startOfWeek, prefix;  
                startOfWeek = moment().startOf('week');  
                prefix = this.unix() < startOfWeek.unix()  ? '[upper]' : '[book]';  
                return this.minutes() === 0 ? prefix + 'dddAh Dot ordering' : prefix + 'dddAh spot mm';  
            },  
            sameElse : 'LL'  
        },  
        ordinalParse: /\d{1,2}(day|month|week)/,  
        ordinal : function (number, period) {  
            switch (period) {  
            case 'd':  
            case 'D':  
            case 'DDD':  
                return number + 'day';  
            case 'M':  
                return number + 'month';  
            case 'w':  
            case 'W':  
                return number + 'week';  
            default:  
                return number;  
            }  
        },  
        relativeTime : {  
            future : '%s within',  
            past : '%s Front',  
            s : 'Seconds',  
            m : '1 Minute',  
            mm : '%d Minute',  
            h : '1 hour',  
            hh : '%d hour',  
            d : '1 day',  
            dd : '%d day',  
            M : '1 Months',  
            MM : '%d Months',  
            y : '1 year',  
            yy : '%d year'  
        },  
        week : {  
            // GB/T 7408-1994 data elements and exchange formats information exchange date and time representation is equivalent to ISO 8601:1988  
            dow : 1, // Monday is the first day of the week.  
            doy : 4  // The week that contains Jan 4th is the first week of the year.  
        }  
    });

Then I started to do it. Open moment.js (version: 2.10.6). The final code is as follows

 // moment.js
// ... the above code is omitted
var Wd = Math.abs,
        Xd = yc("ms"),
        Yd = yc("s"),
        Zd = yc("m"),
        $d = yc("h"),
        _d = yc("d"),
        ae = yc("w"),
        be = yc("M"),
        ce = yc("y"),
        de = Ac("milliseconds"),
        ee = Ac("seconds"),
        fe = Ac("minutes"),
        ge = Ac("hours"),
        he = Ac("days"),
        ie = Ac("months"),
        je = Ac("years"),
        ke = Math.round,
        le = { s: 45, m: 45, h: 22, d: 26, M: 11 },
        me = Math.abs,
        ne = Ha.prototype;
    ne.abs = oc, ne.add = qc, ne.subtract = rc, ne.as = wc, ne.asMilliseconds = Xd, ne.asSeconds = Yd, ne.asMinutes = Zd, ne.asHours = $d, ne.asDays = _d, ne.asWeeks = ae, ne.asMonths = be, ne.asYears = ce, ne.valueOf = xc, ne._bubble = tc, ne.get = zc, ne.milliseconds = de, ne.seconds = ee, ne.minutes = fe, ne.hours = ge, ne.days = he, ne.weeks = Bc, ne.months = ie, ne.years = je, ne.humanize = Fc, ne.toISOString = Gc, ne.toString = Gc, ne.toJSON = Gc, ne.locale = rb, ne.localeData = sb, ne.toIsoString = aa("toIsoString() is deprecated. Please use toISOString() instead (notice the capitals)", Gc), ne.lang = Cd, H("X", 0, 0, "unix"), H("x", 0, 0, "valueOf"), N("x", _c), N("X", bd), Q("X", function(a, b, c) { c._d = new Date(1e3 * parseFloat(a, 10)) }), Q("x", function(a, b, c) { c._d = new Date(q(a)) }), a.version = "2.10.6", b(Da), a.fn = Od, a.min = Fa, a.max = Ga, a.utc = h, a.unix = Zb, a.months = jc, a.isDate = d, a.locale = w, a.invalid = l, a.duration = Ya, a.isMoment = o, a.weekdays = lc, a.parseZone = $b, a.localeData = y, a.isDuration = Ia, a.monthsShort = kc, a.weekdaysMin = nc, a.defineLocale = x, a.weekdaysShort = mc, a.normalizeUnits = A, a.relativeTimeThreshold = Ec;
    var oe = a;
    return oe
});

This code refers to the introduction in the article. It should be caused by the update of moment.js.

In fact, it doesn't matter. Just change return ﹐ to return oe, and at the same time, change motion.definelocale ('zh CN ', {to

OE. Definelocale ('zh CN ', {OK.

Then copy the code of the Chinese Dictionary of moment.js to moment.js, which is in front of the last sentence "return oe;" as shown below

// ... the above code is omitted
var oe = a;
    // Here the moment is changed to oe
    oe.defineLocale('zh-cn', {  
        months : 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),  
        monthsShort : '1 month_2 month_3 month_4 month_5 month_6 month_7 month_8 month_9 month_10 month_11 month_12 month'.split('_'),  
        weekdays : 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),  
        weekdaysShort : 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),  
        weekdaysMin : 'day_One_Two_Three_Four_Five_Six'.split('_'),  
        longDateFormat : {  
            LT : 'Ah spot mm branch',  
            LTS : 'Ah spot m branch s second',  
            L : 'YYYY-MM-DD',  
            LL : 'YYYY year MMMD day',  
            LLL : 'YYYY year MMMD day Ah spot mm branch',  
            LLLL : 'YYYY year MMMD day ddddAh spot mm branch',  
            l : 'YYYY-MM-DD',  
            ll : 'YYYY year MMMD day',  
            lll : 'YYYY year MMMD day Ah spot mm branch',  
            llll : 'YYYY year MMMD day ddddAh spot mm branch'  
        },  
        meridiemParse: /Before dawn|Morning|morning|Noon|Afternoon|Night/,  
        meridiemHour: function (hour, meridiem) {  
            if (hour === 12) {  
                hour = 0;  
            }  
            if (meridiem === 'Before dawn' || meridiem === 'Morning' ||  
                    meridiem === 'morning') {  
                return hour;  
            } else if (meridiem === 'Afternoon' || meridiem === 'Night') {  
                return hour + 12;  
            } else {  
                // 'noon'  
                return hour >= 11 ? hour : hour + 12;  
            }  
        },  
        meridiem : function (hour, minute, isLower) {  
            var hm = hour * 100 + minute;  
            if (hm < 600) {  
                return 'Before dawn';  
            } else if (hm < 900) {  
                return 'Morning';  
            } else if (hm < 1130) {  
                return 'morning';  
            } else if (hm < 1230) {  
                return 'Noon';  
            } else if (hm < 1800) {  
                return 'Afternoon';  
            } else {  
                return 'Night';  
            }  
        },  
        calendar : {  
            sameDay : function () {  
                return this.minutes() === 0 ? '[Today]Ah[Dot ordering]' : '[Today]LT';  
            },  
            nextDay : function () {  
                return this.minutes() === 0 ? '[Tomorrow]Ah[Dot ordering]' : '[Tomorrow]LT';  
            },  
            lastDay : function () {  
                return this.minutes() === 0 ? '[Yesterday]Ah[Dot ordering]' : '[Yesterday]LT';  
            },  
            nextWeek : function () {  
                var startOfWeek, prefix;  
                startOfWeek = moment().startOf('week');  
                prefix = this.unix() - startOfWeek.unix() >= 7 * 24 * 3600 ? '[lower]' : '[book]';  
                return this.minutes() === 0 ? prefix + 'dddAh Dot ordering' : prefix + 'dddAh spot mm';  
            },  
            lastWeek : function () {  
                var startOfWeek, prefix;  
                startOfWeek = moment().startOf('week');  
                prefix = this.unix() < startOfWeek.unix()  ? '[upper]' : '[book]';  
                return this.minutes() === 0 ? prefix + 'dddAh Dot ordering' : prefix + 'dddAh spot mm';  
            },  
            sameElse : 'LL'  
        },  
        ordinalParse: /\d{1,2}(day|month|week)/,  
        ordinal : function (number, period) {  
            switch (period) {  
            case 'd':  
            case 'D':  
            case 'DDD':  
                return number + 'day';  
            case 'M':  
                return number + 'month';  
            case 'w':  
            case 'W':  
                return number + 'week';  
            default:  
                return number;  
            }  
        },  
        relativeTime : {  
            future : '%s within',  
            past : '%s Front',  
            s : 'Seconds',  
            m : '1 Minute',  
            mm : '%d Minute',  
            h : '1 hour',  
            hh : '%d hour',  
            d : '1 day',  
            dd : '%d day',  
            M : '1 Months',  
            MM : '%d Months',  
            y : '1 year',  
            yy : '%d year'  
        },  
        week : {  
            // GB/T 7408-1994 data elements and exchange formats information exchange date and time representation is equivalent to ISO 8601:1988  
            dow : 1, // Monday is the first day of the week.  
            doy : 4  // The week that contains Jan 4th is the first week of the year.  
        }  
    });  
    return oe
});

Use code simple test, and then you can achieve Chinese display.

Posted by hhawkins on Thu, 02 Apr 2020 23:41:43 -0700