admin管理员组

文章数量:1417460

I am trying to format a date with moment.js using the ZH_CN locale. I am using the following code:

moment('2013-12-31T13:21:55+00:00').locale('zh_cn').format("LL h:m:s:SSS")

This returns the following date: 2013年12月31日 1:21:55:000

The time is not correctly formatted however. I was hoping the time would have the appropraite Chinese characters after each digit.I looked within moment-with-locales.js and noticed that the following is defined for the ZH_CN locale:

  relativeTime : {
        future : '%s内',
        past : '%s前',
        s : '几秒',
        m : '1分',
        mm : '%d分',
        h : '1小时',
        hh : '%d小时',
        d : '1天',
        dd : '%d天',
        M : '1个月',
        MM : '%d个月',
        y : '1年',
        yy : '%d年'
    }

These characters are not getting returned in the time even though I specify them in the format string (h:m:s:sss). Anything I am doing wrong? Thanks!

I am trying to format a date with moment.js using the ZH_CN locale. I am using the following code:

moment('2013-12-31T13:21:55+00:00').locale('zh_cn').format("LL h:m:s:SSS")

This returns the following date: 2013年12月31日 1:21:55:000

The time is not correctly formatted however. I was hoping the time would have the appropraite Chinese characters after each digit.I looked within moment-with-locales.js and noticed that the following is defined for the ZH_CN locale:

  relativeTime : {
        future : '%s内',
        past : '%s前',
        s : '几秒',
        m : '1分',
        mm : '%d分',
        h : '1小时',
        hh : '%d小时',
        d : '1天',
        dd : '%d天',
        M : '1个月',
        MM : '%d个月',
        y : '1年',
        yy : '%d年'
    }

These characters are not getting returned in the time even though I specify them in the format string (h:m:s:sss). Anything I am doing wrong? Thanks!

Share Improve this question asked Jan 11, 2016 at 17:02 illwalkwithyouillwalkwithyou 3591 gold badge6 silver badges21 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

The relativeTime customization only affects functions that produce relative time output, such as fromNow.

Additionally, only the localized format specifiers such as LL will contain the characters you're looking for. You can bine them to get the desired output.

var m = moment('2013-12-31T13:21:55+00:00').locale('zh_cn');

m.format("LL")        // "2013年12月31日"
m.format("LLL")       // "2013年12月31日凌晨5点21分"
m.format("LT")        // "凌晨5点21分"
m.format("LTS")       // "凌晨5点21分55秒"
m.format("LL[]LTS")   // "2013年12月31日凌晨5点21分55秒"

I believe the last one will suit your needs. Note the use of [] is necessary just so that LL and LTS are interpreted as separate codes without introducing additional characters.

本文标签: javascriptMomentjs Format time for ZHCN localeStack Overflow