I am trying to convert a date string from the format "YYYYMMDD" to a different format using moment.js.
Here is the code snippet I am using:
import moment from 'moment';
getDateStr(date: string, format){
return moment(date, 'YYYYMMDD').format(format);
}
However, I have encountered an issue where if the date string is partial, it adds "01" for missing parameters.
For instance, when the format is "YYYY-MM-DD" and the input date is "201003", the output becomes "2010-03-01" instead of "2010-03".
Does anyone know how to solve this problem in a more generic way?