Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does angular date filter adding 2 to hour?

Tags:

angularjs

I've created a duration timer in angular js, using angular 'date' filter. For some reason, the hour part starting with '2' instead of '0'.

I'm using the filter like this

 {{runningDuration | date:'HH:mm:ss'}}  

http://jsfiddle.net/rpg2kill/vNdpu/

What am I doing wrong?

like image 604
RPG Avatar asked Dec 26 '22 22:12

RPG


1 Answers

It's because you want to format a date but pass it a difference between dates as an integer. Angular then assumes you want new Date(runningDuration).

You can use a filter to convert the date to utc(this probably requires more corner case handling than just number and date). Demo

JS

myApp.filter('utc', [function() {
    return function(date) {
      if(angular.isNumber(date)) {
        date = new Date(date);
      }
      return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    }   
  } ]);

HTML

{{runningDuration | utc | date:'HH:mm:ss'}}
like image 150
Liviu T. Avatar answered Jan 23 '23 09:01

Liviu T.