JurgenW
JurgenW

Reputation: 317

Format date in local language (moment.js)

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!

Upvotes: 3

Views: 27872

Answers (3)

Gapur Kassym
Gapur Kassym

Reputation: 1201

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.

Upvotes: 3

Jowen
Jowen

Reputation: 5393

Changing the locale globally:

moment.locale('nl');

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

Upvotes: 10

link
link

Reputation: 1676

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

Upvotes: 6

Related Questions