Abdullah Mamun-Ur- Rashid Abdullah Mamun-Ur- Rashid - 1 year ago 87
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();

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

Answer Source

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:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download