Abdullah Mamun-Ur- Rashid Abdullah Mamun-Ur- Rashid - 2 months ago 10
jQuery Question

how to concatenate two digits as string by jquery

I need to convert dd-mm-yyyy to yyyymmdd and put it to another input field of a html form. I used following jquery code for this purpose:

var date = $(this).val();
var d=new Date(date.split("-").reverse().join("-"));
var dd=d.getDate();
var mm=d.getMonth()+1;
var yy=d.getFullYear();
$('#payid').val(mm+dd+yy);


Let a date is
19-09-2016
. I want it to be converted to
20160919
. But it is returning 2044. So, it is adding 19, 09 & 2016. How can I concanate instead of summing the dd, mm & yyyy?

Answer

You don't need to build the string manually at all, you can just split the string, reverse it and re-join it without the - separator:

date.split("-").reverse().join('');

var date = '19-09-2016'; // $(this).val();
var d = date.split("-").reverse().join('')
console.log(d);