title: Javascript: convert date string to date object correctly
body: <p>I have string date for example '20200321082245'. I am trying to convert it by pure Javaccript code to a date object by spliting it and creating an date object
  for example:</p>
  
  <p><strong>'20200321082245' --> 2020-03-21 08:22:45</strong></p>
  
  <p><strong>'202003210.2245'</strong> --> This case with a dot must be excluded. I must return a null date object</p>
  
  <p>How can I convert it correctly ? Since the syncDate could contain a dot also for example a double number also not integer.</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>        function convertDate(syncDate){
              var date = null;
              if(syncDate){
              try {
                  if (!isNaN(syncDate)) {
                          var d = syncDate.toString().match(/.{1,2}/g)
                          var len = d.length;
                          var dateString = '';
                          switch(len){
                              case 2:
                                  dateString = d[0]+d[1];
                              case 3:
                                  dateString = d[0]+d[1] +'-'+d[2]
                              case 4:
                                  dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]
                              case 5:
                                  dateString = d[0]+d[1] +'-'+d[2]+'-'+ d[3]+' '+d[4]
                              case 6:
                                  dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]+' '+d[4]+':'+d[5];
                              case 7:
                                  dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]+' '+d[4]+':'+d[5]+':'+d[6];
                          }
                        if(dateString)
                           date = new Date(dateString);
                      }
                  } catch (e) {}
              }
  
              return date;
           }
  </code></pre>