I have difficulty understanding the use of slice() wrt to the following code, which doesn't fetch me the result I expected.
var cDate = "11-05-2016";
var m = cDate.slice(0,2);
var d = cDate.slice(3,2);
var y = cDate.slice(6);
console.log("Month is " + m);
console.log("Day is " + d);
console.log("Year is " + y);
This gives the following output:
Month is 11
Day is
Year is 2016
I tried to slice with different strings. But every time I do it, it always gives me an empty string when I slice it from the middle of the string. Why is this?
substr.var parts = cDate.split('-'); var m = parts[0]; var d = parts[1]; var y = parts[2];slicewhen you know the indices of the characters in the string, usesubstrwhen you know the length of the substring you want