Input 13.09.2018 (dd.mm.yy)
Expected format 13/09/2018 (dd/mm/yy)
How to convert the input date to expected format in javascript?
Input 13.09.2018 (dd.mm.yy)
Expected format 13/09/2018 (dd/mm/yy)
How to convert the input date to expected format in javascript?
if your object is Date :
the fast way that you can do this is here , if you don't want use another lib :
var newDate = new Date();
newDate.toLocaleDateString('en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
}).replace(/\./g, '/')
result is : "11/09/2018"
and if you have a string , use this regex :
"13.09.2018".replace(/\./g,'/')
result is : "13/09/2018"
Use date.replace(/\./g, '/')
to globally replace the dot(.)
with forward slash.
var date = '13.09.2018';
date = date.replace(/\./g, '/');
console.log(date);
If you think of reuse, create a function to do that for you, but it's not recommended as it's only one line function, but again if you heavily get use of this, you can write something like this:
String.prototype.replaceAll = String.prototype.replaceAll || function(string, replaced) {
return this.replace(new RegExp(string, 'g'), replaced);
};
and simply use it in your code over and over like below:
var str = "13.09.2018";
var res = str.replaceAll(".", "/");