I have a date and a format for that date, given by the user. (d.m.yyyy and 15.10.2021 used for this example)
From that format, I need to get the position of the year, month and day, so that I can use that position on the date.
const year = /y{4}|y{2}/.exec(format);
const month = /m{1,2}/.exec(format);
const day = /d{1,2}/.exec(format);
const yearVal = date.substring(year.index, year.index + year[0].length);
The problem with this is that the format has 1 letter, but my date has 2 numbers. (d and 15.) So i get 0.20
instead of 2021
How can i change this to work with all the different formats?
You can see all the possible formats here: node-dateformat
For example: the difference between "d" and "dd" are like this:
"d" Day of the month as digits; no leading zero for single-digit days.
"dd" Day of the month as digits; leading zero for single-digit days.