Reputation: 856
I have a date
string 11/2004
that comes to me via an API and on mobile I am trying to display this on a date
field.
If i try to render this as it is I see the following warning on console
The specified value "11/2004" does not conform to the required format. The format is "yyyy-MM" where yyyy is year in four or more digits, and MM is 01-12.
so using momentjs i thought this should be easy to change to date or month format but i am struggling with it as i cant seem to get the output i want.
I tried the following code but that give me invalid date
moment('11/2004').format('yyyy-MM')
What am i doing wrong here? How do i display the date on an input field in the correct format? I cant change the field type to anything else as its a date picker field on mobile.
Upvotes: 2
Views: 2299
Reputation: 6739
Looking at the documentation on parsing what you need to do is created your moment object with the format specified.
moment('11/2004','MM/YYYY')
This will set the day to the first, but the month and the year to the ones specified.
Also, your format is wrong, the yyyy
should be capital YYYY
So your final code should look like this
moment('11/2004', 'MM/YYYY').format('YYYY-MM')
Upvotes: 2
Reputation: 13943
You should create your date with the string-format
moment('11/2004', 'MM/YYYY')
and then display it using the format()
function
moment('11/2004', 'MM/YYYY').format('YYYY-MM')
Upvotes: 1