Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert UTC to local time using angularjs

Tags:

angularjs

As a response from the json I am getting the UTC timezone. I need to convert it to local time.

<span class="text-muted">{{trans.txnDate}}</span>

Can anyone help on this?

like image 274
chandru Avatar asked Feb 19 '15 09:02

chandru


People also ask

How do you convert UTC time to local time zone in angular?

The ToLocalTime method converts a DateTime value from UTC to local time. To convert the time in any designated time zone to local time, use the TimeZoneInfo. ConvertTime method. The value returned by the conversion is a DateTime whose Kind property always returns Local.

How do you convert UTC time to local time?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

Does angular Date pipe convert to local time?

Angular 4's DatePipe supports the UTC zone designator suffix ('Z'), so if the value you pass to the pipe is a UTC datetime, then your backend service can return your datetime with the 'Z' zone designator or you can append Z and it will show the date time in the user's local time.


2 Answers

I just had to solve this problem as well with dates coming from .NET Web API in the format 'yyyy-MM-ddTHH:mm:ss' (e.g. 2016-02-23T00:11:31) without the 'Z' suffix to indicate UTC time.

I created this filter that extends the angular date filter and ensures that the timezone suffix is included for UTC time.

UTC to Local Filter:

(function () {
    'use strict';

    angular
        .module('app')
        .filter('utcToLocal', utcToLocal);

    function utcToLocal($filter) {
        return function (utcDateString, format) {
            if (!utcDateString) {
                return;
            }

            // append 'Z' to the date string to indicate UTC time if the timezone isn't already specified
            if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) {
                utcDateString += 'Z';
            }

            return $filter('date')(utcDateString, format);
        };
    }
})();

Example Usage:

{{product.CreatedDate | utcToLocal:"dd.MM.yyyy"}}
like image 66
Jason Watmore Avatar answered Sep 21 '22 10:09

Jason Watmore


EDIT (2nd Jan 2017): Please refer @Jason's answer, it is better than this one since it uses custom filter to fix the date format - that's the more Angular way of doing it.


My original answer and edits:

You could use the date filter to format the date:

<span class="text-muted">{{trans.txnDate | date:'yyyy-MM-dd HH:mm:ss Z' }}</span>

This will output:

2010-10-29 09:10:23 +0530

(assuming trans.txnDate = 1288323623006;)

See this documentation of date in angularjs.org. It has quite a few examples that are very helpful!


EDIT:

In response to your comment, use the following to get the date as 17 oct 2014:

<span class="text-muted">{{trans.txnDate | date:'dd MMM yyyy' | lowercase }}</span>

Check the documentation link that I mentioned above.

EDIT2:

In response to your other comment, use the following code. The problem is that the string that you are getting is not properly formatted so the Date object is not able to recognise it. I have formatted it in the controller and then passed to the view.

function MyCtrl($scope) {
  var dateString = "2014:10:17T18:30:00Z";
  dateString = dateString.replace(/:/, '-'); // replaces first ":" character
  dateString = dateString.replace(/:/, '-'); // replaces second ":" character
  $scope.date = new Date(dateString);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app ng-controller="MyCtrl">
  {{date | date:'dd MMM yyyy' | lowercase }}
</div>

The JS code for replacement can be improved by finding a smarter way to replace the first 2 occurrences of : character.

like image 34
Rahul Desai Avatar answered Sep 23 '22 10:09

Rahul Desai