title: Auto format input to date using javascript
body: <p>I have some javascript that autoformats user input into an SSN (ddd-dd-dddd)
  I'm having trouble converting this script to support a date format (mm/dd/yyyy)</p>
  
  <pre><code>    var val = this.value.replace(/\D/g, '');
      var newVal = '';
      if (val.length &gt; 4) {
          this.value = val;
      }
      if ((val.length &gt; 3) &amp;&amp; (val.length &lt; 6)) {
          newVal += val.substr(0, 3) + '-';
          val = val.substr(3);
      }
      if (val.length &gt; 5) {
          newVal += val.substr(0, 3) + '-';
          newVal += val.substr(3, 2) + '-';
          val = val.substr(5);
      }
      newVal += val;
      this.value = newVal;
  </code></pre>
  
  <p>Would someone be able to explain how this is working and show me how to convert it to my date format?</p>