Hi I have a few syntax problems as I build a date from a string that looks like
My code:
paramCheckDate = "01/14/2022"
CheckDateYYYYMMDD = paramCheckDate.split["/"][2].toString() + paramCheckDate.split["/"][1].toString() + paramCheckDate.split["/"][0].toString()
I would like to build another string in the format YYYYMMDD. Splitting by index and then.toString but I have syntax errors if I can get some guidance that would be awesome!
Square braces are for array access, round braces are for function calls.
Use paramCheckDate.split("/")
['/']
instead of ('/')
- String.prototype.split
is a function that you have to call, not an array/object that you have to index.toString()
, the parts of the strings are already - well - strings.split
3 times. Instead, you can split it once and then join:const [m, d, y] = paramCheckDate.split('/')
const CheckDateYYYYMMDD = y + m + d
You can use the below Regular Expression to replace your source string to your required format
const CheckDateYYYYMMDD = paramCheckDate.replace(/(\d+)\/(\d+)\/(\d+)/,"$3$1$2");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.