Trent Trent - 2 months ago 14
AngularJS Question

Angular js : convert date to another timezone

I have to convert my date to different timezone based on the logged user timezone

I have tried

$scope.currentDate = Date.now();
$scope.tz = 'America/New_York';


In view i have called like this
{{currentDate | date:'h:mm a' : t}}


But it's not working.,..how could i achieve this?

Answer

Make use of Moment timezone JS

var app = angular.module('app', []);

app.controller('mainCtrl', function($scope) {
  var now = moment(new Date());
  $scope.currentDate = now.tz('America/New_York').format('hh:mm a');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.4/moment-timezone-with-data-2010-2020.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<div ng-app='app'>
  <div ng-controller='mainCtrl'>
    <h4>{{ currentDate }}
</h4>
  </div>
</div>