I want to convert the string "19-12-2018" to "2018-12-19" How can I do it?
var str = "19-12-2018";
str.split('').reverse().join('') //returns 8102-21-91"
how to do this?
I want to convert the string "19-12-2018" to "2018-12-19" How can I do it?
var str = "19-12-2018";
str.split('').reverse().join('') //returns 8102-21-91"
how to do this?
var str = "19-12-2018";
var newstr = str.split('-').reverse().join('-');
console.log(newstr);
Do split('-')
first:
var str = "19-12-2018";
str = str.split('-').reverse().join('-');
console.log(str);
I call the split function passing dash which separate each part of the string
str.split("-").reverse().join("-");
Description of functions used
String.prototype.split()
: The split() method turns a String into an array of strings, by separating the string at each instance of a specified separator string. const chaine = "Text";
console.log(chaine.split('')); // output ["T", "e", "x", "t"]
Array.prototype.reverse()
: The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.const characters = ["T", "e", "x", "t"];
console.log(characters.reverse()); // output ["t", "x", "e", "T"]
Array.prototype.join()
: The join() method creates and returns a new string by concatenating all of the elements in an arrayconst reverseCharacters = ["t", "x", "e", "T"];
console.log(reverseCharacters.join('')); // output "txeT"
Try this one
var str = "19-12-2018".split('-');
var newstr=str[2]+"-"+str[1]+"-"+str[0];