12

I use below code to print the time duration:

{{(endTime - startTime) * 1000 | date: 'H:mm:ss'}}

But I get the time of GMT +8

AngularJS has any way to print out the non-convert duaration, Or I need to use this way to do this?

{{(endTime - startTime) % (60 * 60 * 24) / (60 * 60) | number: 0}}:
{{(endTime - startTime) % (60 * 60) / 60 | number: 0}}:
{{(endTime - startTime) % 60 | number: 0}}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63

4 Answers4

26

In AngularJS 1.3 and up, there is optional timezone argument in date filter. The doc says If the timezone is not specified, the timezone of the browser will be used.

https://code.angularjs.org/1.3.0/docs/api/ng/filter/date

Adding 'UTC' timezone will print the duration.

{{(endTime - startTime) | date: 'H:mm:ss': 'UTC'}}
Aung Khaing Oo
  • 261
  • 3
  • 2
  • 1
    Not sure why nobody upvoted that answer before, but it's brilliant and works perfectly. Many thanks. – ereOn Jan 28 '16 at 19:31
  • Not working for me in case of need to show days also. Using this solution adds 1 day cause "dd" starts on "01". Need to check if duration is >=1 in order to substract 1 day duration (24*60*60*1000). Now it works! Thanks for inspiration – Hugo Sep 28 '16 at 08:50
4

The best way is to use moment.js library. There are many features in it that would be useful for application improvement.

Alex_Crack
  • 710
  • 1
  • 4
  • 14
3

The date filter formats a Unix timestamp (in milliseconds), so it doesn't work on durations.

You'll have to do manual formatting, like you suggest.

jkinkead
  • 4,101
  • 21
  • 33
1

you can use this bower component to format by duration.

oshai
  • 14,865
  • 26
  • 84
  • 140