Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify timezone to date filter

Tags:

angularjs

I have this date: 1386800070703. Let's say it's UTC.
How can I tell angular what timezone it is and in what timezone I want it displayed?

I'm currently using tihs which only converts it without any timezone info:
{{1386800070703 | date:'d MMMM yyyy'}}

And would like to be able to show a timezone to the right of the date.

like image 845
Francisc Avatar asked Dec 11 '13 22:12

Francisc


People also ask

How do I filter by date in Excel?

Navigate to a PivotTable or PivotChart in the same workbook. Add a column from the Date table to the Column Labels or Row Labels area of the Power Pivot field list. Click the down arrow next to Column Labels or Row Labels in the PivotTable. Point to Date Filters, and then select a filter from the list.

How do I filter data by date in react?

Example: Date Filter The Date column is using a Date Filter. A custom comparator is provided to parse the data and allow date comparisons to be made. The native date picker is forced to be used in every browser. The minimum valid year is set to 2000 , and maximum valid year is 2021 .


2 Answers

In the next version of AngularJS (1.3.0) you can specify the timezone this way:

{{someDate | date:'d MMMM yyyy Z' : 'UTC'}}

Currently, only supports UTC, as you can see in the docs: https://code.angularjs.org/1.3.0-rc.2/docs/api/ng/filter/date

You can play with it here: Plunker example

Notice, the time zone will be shown as local-time even if specified otherwise.

like image 79
nir Avatar answered Oct 09 '22 02:10

nir


Here's a directive I wrote that does just that:

angular.module('CommonDirectives', []).directive('adjustTimezone', ['$filter', function ($filter) {
  return {
    restrict: 'A',
    scope: {
      adjustTimezone: '@'
    },
    link: function ($scope, element, attrs) {
      var date = new Date(attrs.adjustTimezone);

      date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
      element.text($filter('date')(date, attrs.filter));
    }
  }
}]);

Here's how you use it:

<span data-adjust-timezone="{{date.MilitaryTime}}" data-filter="EEEE, MMMM d, y h:mm a"></span> 
like image 40
parliament Avatar answered Oct 09 '22 02:10

parliament