Moment js multiple formats

Viewed 588

I'm trying to get moment to work without 0 padding or with 0 padding.

Working example:

const dbd = '01';
const dbm = '19';
const dby = '89';
const m = moment(`${dbd}${dbm}${dby}`, 'DD MM YY');

The above works great. But if I pass dbd = '1' then it says invalid. Users in my input field could enter the 0 or not. Is there a way to handle this in moment ?

I tried using

const m = moment(`${dbd}${dbm}${dby}`, 'D M YY');

But that didn't work either.

1 Answers

You just need space between each values in the input:

moment(`${1} ${4} ${2005}`, 'DD MM YY').format('llll');
// <= "Fri, Apr 1, 2005 12:00 AM" 

Beacause it can't distinguish between different parts of the input if they are continuous(without space between them).

Related