Question

Why do I get "invalid date" when I try the month "may"? (it is Dutch, so that's why there is "mei")

I don't get "invalid date" when I try the month "june" (in Dutch: "juni")

I don't get it... Check the fiddle: Fiddle

Script: (Purely for testing purposes, to show that the month "may" does not work..)

$(document).ready(function () {
    var newvartitle = "5 mei 2014";
    var goodformatdata = moment(newvartitle).format('DD/MM/YYYY');
    var newvartitle2 = "5 juni 2014";
    var goodformatdata2 = moment(newvartitle2).format('DD/MM/YYYY');
    $('.tester').append(goodformatdata);
    $('.tester').append(goodformatdata2);
});

This problem is solved, the solution: http://jsfiddle.net/kULx3/2/

Était-ce utile?

La solution

The problem is that the language that you're using is the output language. For example if you try this

alert(moment("5 5 2014").lang("nl").format("DD/MMMM/YYYY"));

the output will be

05/mei/2014

In order to accept inputs in Dutch you will have to implement it. For example create a method which takes a string as a parameter and changes Dutch months to numbers, so when you call

moment(toMoment("5 mei 2014")).lang("nl").format("DD/MMMM/YYYY")

the return value of toMoment("5 mei 2014") would be 5 5 2014

An example of that would be

function toMoment(str){

    var result = null;
    var splitted = str.split(" ");
    var month = splitted[1];

    switch(month){
        case "mei": result = "5"; break;
    }

    return splitted[0] + " " + result + " " + splitted[2];
}

Autres conseils

By default, Moment.js is set to English. This can be changed by passing the desired language into Moment.js's .lang() function:

moment.lang('en');

In English, the "i" in "juni" is being ignored completely. Moment.js only looks at the first three letters of the month name. "Juni" is being treated as "jun" ("June" in English) whereas there is no three-letter month which begins with "mei" in English.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top