STACK2
STACK2

Reputation: 163

how to convert GMT time to ist time in javascript

i'm getting time like this - 2018-06-08T08:52:51.871Z. i want to convert it like 08-06-2018 2.42 PM. how to convert GMT to IST using angular js.

html

 <td>{{names.timestamp}}</td>

Javascript

<script>
    function base64toHEX(base64) {
        var raw = atob(base64);
        var HEX = '';
        for (i = 0; i < raw.length; i++) {
            var _hex = raw.charCodeAt(i).toString(16)
            HEX += (_hex.length == 2 ? _hex : '0' + _hex);
        }
        return HEX.toUpperCase();
    }
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope, $http) {
        $http.get('url', {
            headers: { 'Authorization': 'Basic a2VybmVsc3BoZ==' }
        })
           .then(function (response) {
               $scope.names = response.data;
               $scope.decodedFrame =base64toHEX($scope.names.dataFrame);
        });
    });
</script>

Upvotes: 0

Views: 3366

Answers (5)

Aleksey Solovey
Aleksey Solovey

Reputation: 4191

You can use a built-in filter for dates:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.timestamp = new Date("2018-06-08T08:52:51.871Z");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

  GMT: {{timestamp | date: 'dd-MM-y h:mm a'}}
  <hr>
  IST: {{timestamp | date: 'dd-MM-y h:mm a' : '+0430'}}

</div>

Upvotes: 0

shutsman
shutsman

Reputation: 2510

also you can check https://www.w3schools.com/angular/ng_filter_date.asp and show date what you want

{{names.timestamp | date : "dd-MM-y hh.mm a"}}

Upvotes: 0

Vladu Ionut
Vladu Ionut

Reputation: 8193

You can do this with the angularJS Pipe

{{ names.timestamp | date : 'mm-dd-yyyy hh.mm a'}}}

Upvotes: 0

DsRaj
DsRaj

Reputation: 2328

use the angular filter to format the date and timezone

<td>{{names.timestamp | date:'MM-dd-yyyy HH:mm:ss'| timezone: '+0430'}}</td>

Upvotes: 1

Akhil Aravind
Akhil Aravind

Reputation: 6130

use angular pipe In HTML Template Binding

{{ date_expression | date : format : timezone}}

Upvotes: 0

Related Questions