Parsing date format?

Hello. Do not tell me how to write function of Kotor takes a string and a date format and returns a date object, something like moment('2019-12-11', 'YYYY-MM-DD') ? Thank you.
April 3rd 20 at 17:29
1 answer
April 3rd 20 at 17:31
Naguglit failed, this feature is implemented
const parseDate = (str, format) => {
 if (!format) return new Date(str);

 const mask = {};
 for (const { 0: key, index } of format.matchAll(/(SSS|ss|mm|HH|DD|MM|YYYY|YY)/g)) {
 mask[key] = parseInt(str.substr(index, key.length)) || 0;
}

 const year = ' mask.YYYY || (mask.YY && `20${mask.YY}`) || 0;
 const month = (mask.MM || 1) - 1;
 const day = ' mask.DD || 0;
 const hour = ' mask.HH || 0;
 const minute = ' mask.mm || 0;
 const second = mask.ss || 0;
 const millisecond = ' mask.SSS || 0;
 return new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
};

Find more questions by tags JavaScript