40

I'm using the date filter to render a unix timestamp in a certain format. I've noticed the filter adds the local timezone to the output.

Is there any way to simply output the exact timestamp, without adding any timezone information?

Input:

talk.content.date_and_time = 1400167800 

(is 05 / 15 / 14 @ 3:30:00pm UTC)

Code:

{{talk.content.date_and_time*1000 | date:'dd-M-yyyy H:mm Z'}}

Output:

15-5-2014 17:30 +0200

How can I make the output 15:30 instead of 17:30?

4
  • What's your code? What's the input, what's the output? What's the desired output? Commented Feb 14, 2014 at 15:15
  • Sorry, I was in a hurry. Added to the question @JBNizet. Commented Feb 14, 2014 at 16:04
  • if you pass a raw integer number of milliseconds, the Date filter will interpret it with respect to local time still. However using the UTC constructor and passing a date or pre-empting the filter's timezone offset by adding the offset to your own millis and passing it seems to work. Commented Feb 14, 2014 at 20:48
  • 1
    possible duplicate of Using AngularJS date filter with UTC date Commented Feb 27, 2014 at 15:15

4 Answers 4

97

Since version 1.3.0 AngularJS introduced extra filter parameter timezone, like following:

{{ date_expression | date : format : timezone}}

But in versions 1.3.x only supported timezone is UTC, which can be used as following:

{{ someDate | date: 'MMM d, y H:mm:ss' : 'UTC' }}

Since version 1.4.0-rc.0 AngularJS supports other timezones too. I was not testing all possible timezones, but here's for example how you can get date in Japan Standard Time (JSP, GMT +9):

{{ clock | date: 'MMM d, y H:mm:ss' : '+0900' }}

Here you can find documentation of AngularJS date filters.

NOTE: this is working only with Angular 1.x

Here's working example

Sign up to request clarification or add additional context in comments.

5 Comments

Sadly this additional timezone parameter is not supported in Angular 2's Date pipe. Doing something like what is suggested in the first answer will still work in Angular 2.
@user3640967 this works only for Angular 1.x, but not for Angular 2
what to do for Angular2 (Local date to UTC), i search lot but could not reach to conclusion , so if you guide me then i will help full.
For Angular2 you have to create controller or utility function to convert date to UTC and back. You can check accepted answer for example on how to do it.
Also probably works for IANA timezones such as 'Europe/Berlin'
31

The 'Z' is what adds the timezone info. As for output UTC, that seems to be the subject of some confusion -- people seem to gravitate toward moment.js.

Borrowing from this answer, you could do something like this without moment.js:

controller

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

app1.controller('ctrl',['$scope',function($scope){

  var toUTCDate = function(date){
    var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    return _utc;
  };

  var millisToUTCDate = function(millis){
    return toUTCDate(new Date(millis));
  };

    $scope.toUTCDate = toUTCDate;
    $scope.millisToUTCDate = millisToUTCDate;

  }]);

template

<html ng-app="app1">

  <head>
    <script data-require="angular.js@*" data-semver="1.2.12" src="http://code.angularjs.org/1.2.12/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-controller="ctrl">
      <div>
      utc {{millisToUTCDate(1400167800) | date:'dd-M-yyyy H:mm'}}
      </div>
      <div>
      local {{1400167800 | date:'dd-M-yyyy H:mm'}}
      </div>
    </div>
  </body>

</html>

here's plunker to play with it

See also this and this.

Also note that with this method, if you use the 'Z' from Angular's date filter, it seems it will still print your local timezone offset.

2 Comments

rather than a function in the controller, you could, as others have proposed, incorporate millisToUTCDate into a filter.
You can also merge this two functions into one. var millisToUTCDate = function(millis){ var date new Date(millis); return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); }; This was helpful for me cos' I wasn't able to make filters work :/. Thanks.
0

The date filter always formats the dates using the local timezone. You'll have to write your own filter, based on the getUTCXxx() methods of Date, or on a library like moment.js.

Comments

0

I just used getLocaleString() function for my application. It should adapt the timeformat common to the locale, so no +0200 etc. Ofcourse, there will be less possibility for controlling the width of your string then.

var str = (new Date(1400167800)).toLocaleString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.