title: Transpose php datetime+timezone function into js function
body: <p>here is a usefull function I have in my solution</p>
  
  <pre><code>public static function formatTime($time, $timezone)
  {
      $timezone = new \DateTimeZone($timezone);
      $time = $time-&gt;setTimezone($timezone);
      return \Locale::getDefault() == 'fr' ? date('H:i', $time-&gt;getTimestamp() + $time-&gt;getOffset()) : date('g:i a', $time-&gt;getTimestamp() + $time-&gt;getOffset());
  }
  </code></pre>
  
  <p>I store my dates in UTC for my items, and the container of these items has a defined timezone. With this function, I apply the timezone to all my items.</p>
  
  <p>I need now to "translate" this function into a JS one doing exactly the same (in fact, my items are loaded by PHP but then new items directly come by JS and a need to apply homegeneously this same function in JS)</p>
  
  <p>Currently, I've got this one (the previous one, without timezone considerations unfortunately):</p>
  
  <pre><code>        Twig.setFunction('format_time', function(value) {
          // http://stackoverflow.com/questions/5324178/javascript-date-parsing-on-iphone
          var arr = value.date.split(/[- :]/);
          var d = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
  
          {% if app.request.getLocale() == "fr" %}
              return (d.getHours() &lt; 10 ? '0' + d.getHours() : d.getHours()) + ':' + (d.getMinutes() &lt; 10 ? '0' + d.getMinutes() : d.getMinutes());
          {% else %}
              if (d.getHours() &gt;= 12) {
                  var hour = d.getHours() - 12;
                  hour = hour &lt; 10 ? '0' + hour : hour;
                  var a = 'pm';
              } else {
                  var hour = d.getHours();
                  hour = hour &lt; 10 ? '0' + hour : hour;
                  var a = 'am';
              }
              return hour + ':' + (d.getMinutes() &lt; 10 ? '0' + d.getMinutes() : d.getMinutes()) + ' ' + a;
          {% endif %}
      });
  </code></pre>
  
  <p>Do you have un idea on how implement the timezone gestion easily in JS by passing the string 'America/New_York' or 'Europe/Paris' in a second argument?</p>
  
  <p>Thanx a lot!!</p>