User-330142929 posted
Hi Sidu,
According to your description, I think you convert string to data by adding a string prototype function.
I suggest that you could achieve the function you want by adding the following function to the Date prototype.
I have made a demo, wish it is useful to you.
<script>
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //MM
"d+": this.getDate(), //dd
"h+": this.getHours(), //hh
"m+": this.getMinutes(), //mm
"s+": this.getSeconds(), //ss
"q+": Math.floor((this.getMonth() + 3) / 3), //qq
"S": this.getMilliseconds() //S
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var d1=new Date(2018,03,22,12,34,28).Format("dd/MM/yyyy");
var d2=new Date(2018,03,22,12,34,28).Format("dd/MM/yyyy hh:mm:ss");
alert(d1+"\n"+d2);
</script>
How It works.

Please feel free to let me know, if you have any question.
Best Regards,
Abraham.