Reputation: 6072
I am validating a date in moment.js in format D. MMM YYYY
and then converting it to format YYYY-MM-DD
. While validating it keeps on giving me invalid date. Below is the code.
var date = '26. Mär 1995';
var mom = moment(date, "D. MMM YYYY","de", true);
if (mom.isValid()) {
console.log(moment(date, 'D. MMM YYYY').format('YYYY-MM-DD'));
} else {
console.log('invalid date')
}
Upvotes: 0
Views: 1001
Reputation: 135
First according to the "de" locale file in the momentJS repository on Github, the shortMonth for März is Mrz. (yes with a dot .) check code below (from the same file):
monthsShort : 'Jan._Febr._Mrz._Apr._Mai_Jun._Jul._Aug._Sept._Okt._Nov._Dez.'.split('_')
Second, for your date, you don't need to reparse it, just format your moment directly:
mom.format('YYYY-MM-DD');
Putting everything together:
var date = "26. Mrz. 1995";
var mom = moment(date, "D. MMM YYYY", "de", true);
if (mom.isValid()) {
console.log(mom.format("YYYY-MM-DD");
} else {
console.log("invalid date");
}
Upvotes: 2