Inside my JSON there are three different types of date values:
D.M.YYYY
DD.M.YYYY
D.MM.YYYY
.
How would you suggest me to standardize this to YYYY.MM.DD
format? (like 2022.02.02 instead of 2.02.2022 or 02.2.2022 or 2.2.2022)?
Inside my JSON there are three different types of date values:
D.M.YYYY
DD.M.YYYY
D.MM.YYYY
. How would you suggest me to standardize this to YYYY.MM.DD
format? (like 2022.02.02 instead of 2.02.2022 or 02.2.2022 or 2.2.2022)?
dmy="9.8.2034".split(".");
result = ("0000" + dmy[2]).slice(-4) +"."
+ ("00" + dmy[1]).slice(-2) +"."
+ ("00" + dmy[0]).slice(-2);
// result = "2034.08.09"
While this is my general approach, in your case, if you are certain that the D and M fields will definitely always contain at least 1 digit, and the Y field will always be a full 4-digit year, you could chop off some of the extra "0"s you are adding.
result = dmy[2]+"."
+ ("0" + dmy[1]).slice(-2) +"."
+ ("0" + dmy[0]).slice(-2);
Personally, however, I would suggest using my more verbose first method above, as it will be easier for you to recognise what your intention was when you are later re-reading your code.