You can parse string with multiple formats as described here.
If you need you can also pass locale parameter.
The following code shows a possible solution to your question:
var arr = ['31/10/93','10/31/93','93/10/31'];
for(var i=0; i< arr.length; i++){
var m = moment.utc(arr[i], ['DD/MM/YY', 'MM/DD/YY', 'YY/MM/DD']);
console.log(m.format('DD MMMM YYYY')); // 31 October 1993
}
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
This works fine for the input you gave as example, but may give you unexepcted results for inputs like 01/02/93. This string can represent both 1 February 1993 and 2 January 1993.
The suggested code will show 01 February 1993 because, as stated in the docs:
Moment uses some simple heuristics to determine which format to use. In order:
- Prefer formats resulting in valid dates over invalid ones.
- Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
- Prefer formats earlier in the array than later
If you need a JavaScript Date from moment object you can use toDate() method, while if you need Unix timestamp you can use valueOf() or unix().
After your comment, I can suggest to get "localized" format using moment.localeData(). Using longDateFormat('L') you can get localized format string and then use it in the parsing function. For example, longDateFormat('L') will return MM/DD/YYYY for us locale and DD/MM/YYYY for it. Code sample:
var s = '01/02/93';
function formatSample(s){
var ldf = moment.localeData().longDateFormat('L');
return moment.utc(s, [ldf, 'YY/MM/DD']).format('DD MMMM YYYY');
}
moment.locale('us');
formatSample(s); // 02 January 1993
moment.locale('it');
formatSample(s); // 01 febbraio 1993
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.min.js"></script>