最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Format date in local language (moment.js) - Stack Overflow

programmeradmin3浏览0评论

Can you help me in formatting a UTC date into a textual representation in a local language (Dutch to be specific).

The code

var realDate = moment.utc(birthday);
var now = moment();
birthdayToday = (realDate.month() == now.month() && realDate.date() == now.date());
        data.Birthdays.push({
            Name: preferredName,
            Birthday: birthdayToday ? 'Today!' : realDate.format("MMMM D"),
            Path: path,
            PhotoUrl: photoUrl,
            AccountName: accountName,
            BirthdayIsToday: birthdayToday
        });

Because of the line realDate.format("MMMM D"), this current displays as May 31, June 6 etc. What I want is 31 Mei, 6 Juni (dutch dates).

I dont see a clear example in the documentation on how to use format with a local language

Any help appreciated!

Can you help me in formatting a UTC date into a textual representation in a local language (Dutch to be specific).

The code

var realDate = moment.utc(birthday);
var now = moment();
birthdayToday = (realDate.month() == now.month() && realDate.date() == now.date());
        data.Birthdays.push({
            Name: preferredName,
            Birthday: birthdayToday ? 'Today!' : realDate.format("MMMM D"),
            Path: path,
            PhotoUrl: photoUrl,
            AccountName: accountName,
            BirthdayIsToday: birthdayToday
        });

Because of the line realDate.format("MMMM D"), this current displays as May 31, June 6 etc. What I want is 31 Mei, 6 Juni (dutch dates).

I dont see a clear example in the documentation on how to use format with a local language

Any help appreciated!

Share Improve this question asked May 27, 2014 at 13:43 JurgenWJurgenW 3172 gold badges5 silver badges18 bronze badges 2
  • See the documentation for internationalization: momentjs.com/docs/#/i18n – Chris Commented May 27, 2014 at 13:48
  • Consider realDate.lang('nl') and realDate.format('D MMMM'). It seems you'll have to add support for Dutch (nl) though as it doesn't seem to have default support. – RobG Commented May 27, 2014 at 14:08
Add a comment  | 

3 Answers 3

Reset to default 10

Changing the locale globally:

moment.locale('nl');

And make sure you also load the moment-with-locales.(min.)js file

Something like this?

var today = moment()
today.lang('de')
console.debug(today.calendar())
console.debug(moment().calendar())

Result:

Heute um 15:54 Uhr
Today at 3:54 PM

Remember to include moment-with-langs.js instead of the simple moment.js. Also remember that .lang provides instance specific configuration, so you will have to call .lang('de') for each moment instance that you want to use in German.

Or if you want global configuration:

moment.lang('de') //<-- call not on the instance, but on the moment function
var today = moment()
console.debug(today.calendar())
console.debug(moment().calendar())

Result:

Heute um 15:54 Uhr
Heute um 15:54 Uhr

Moment with langs CDN

You can change moment locale globally like that:

import moment from 'moment';
import localization from 'moment/locale/fr';

moment.locale('fr', localization);

I hope this helps you.

发布评论

评论列表(0)

  1. 暂无评论