title: Handling date strings with various length
body: <p>I'm trying to convert dates with different formats to a unified format.
  The data I get can be <code>MM/DD/YYYY</code> or <code>M/DD/YYYY</code> or <code>MM/D/YYYY</code> or <code>M/D/YYYY</code>.</p>
  
  <p>As of now I can handle all except <code>M/D/YYYY</code>. 
  Does anyone know how to solve that?</p>
  
  <p>EDIT: Realized I didn't clarify which format I try to get, it's DD/MM/YYYY </p>
  
  <p>My code as it is (not sure if it's the most efficient way):</p>
  
  <pre><code>var str = "2/13/2016"; // MM/DD/YYYY
  
  var day = str.substr(3,2);
  var month = str.substr(0,2);
  var year = str.substr(6,4);
  
  if(month.indexOf('/') &gt; -1){
      month = month.replace('/','');
      var newM = '0' + month;
      day = str_date.substr(2,2);
  
      return day + '-' + newM + '-' + year;
  }
  else if(day.indexOf('/') &gt; -1){
      day = day.replace('/','');
      var newD = '0' + day;
      year = str.substr(5,5);
  
      return newD + '-' + month + '-' + year;
  }
  else {
      return  day + '-' + month + '-' + year;
  }
  </code></pre>