title: Javascript date format for a date
body: <p>How do I format a date in Javascript to something e.g. 'yyyy-MM-dd HH:mm:ss z'?</p>
  
  <p>This <code>date.toString('yyyy-MM-dd HH:mm:ss z');</code> never work out for me :/</p>
  
  <p>Any idea?</p>
  
  <p>======</p>
  
  <p>I solved my own which I rewrote like this:</p>
  
  <pre><code>var parseDate = function(date) {
      var m = /^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d) UTC$/.exec(date);
      var tzOffset = new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTimezoneOffset();
  
      return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5] - tzOffset, +m[6]);
  }
  
  var formatDateTime = function(data) {
      var utcDate = parseDate(data);
  
      var theMonth = utcDate.getMonth() + 1;
      var myMonth = ((theMonth &lt; 10) ? "0" : "") + theMonth.toString();
  
      var theDate = utcDate.getDate();
      var myDate = ((theDate &lt; 10) ? "0" : "") + theDate.toString();
  
      var theHour = utcDate.getHours();
      var myHour = ((theHour &lt; 10) ? "0" : "") + theHour.toString();
  
      var theMinute = utcDate.getMinutes(); 
      var myMinute = ((theMinute &lt; 10) ? "0" : "") + theMinute.toString();
  
      var theSecond = utcDate.getSeconds(); 
      mySecond = ((theSecond &lt; 10) ? "0" : "") + theSecond.toString();
  
      var theTimezone = new Date().toString();
      var myTimezone = theTimezone.indexOf('(') &gt; -1 ? 
              theTimezone.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') : 
              theTimezone.match(/[A-Z]{3,4}/)[0];
  
      if (myTimezone == "GMT" &amp;&amp; /(GMT\W*\d{4})/.test(theTimezone)) {
          myTimezone = RegExp.$1;
      }
  
      if (myTimezone == "UTC" &amp;&amp; /(UTC\W*\d{4})/.test(theTimezone)) {
          myTimezone = RegExp.$1;
      }
  
      var dateString = utcDate.getFullYear() + "-" + 
                       myMonth + "-" +
                       myDate + " " +
                       myHour + ":" +
                       myMinute + ":" +
                       mySecond + " " +
                       myTimezone;
  
      return dateString;
  }
  </code></pre>
  
  <p>and I get: 2012-11-15 22:08:08 MPST :) PERFECT!</p>