Utilize the moment(String, String)
function, with DD
representing the day of the month, MM
for the month number, and YYYY
for a 4 or 2 digit year.
Keep in mind that if your input doesn't follow recognized ISO 8601 or RFC 2822 formats, it's advisable to use moment(String, String)
instead of moment(String)
for consistent results on different browsers.
When converting a string into a moment, the process involves checking if it matches known ISO 8601 formats first, then the RFC 2822 Date time format before resorting to new Date(string)
when a recognized format isn't identified.
Attention: Browser support for parsing strings varies widely as there is no set standard for supported formats. Thus, what works in one browser may not work in another.
To ensure consistent results when parsing non-ISO 8601 strings, prefer using String + Format.
Check out a live example below:
["10-05-2018","06/08/2018", "01 03 2018"].forEach( (item) => {
console.log( moment(item, 'DD MM YYYY').format() );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>