-3

I have an api returning timestamp variable. When I print in AngularJs it shows as 1488195848000.But I want that to be displayed as 2017-02-28 14:49:48(In Timestamp format)

How do I convert in controller?

Anusha Bayya
  • 103
  • 1
  • 5
  • 17
  • Possible duplicate of [AngularJS - convert dates in controller](http://stackoverflow.com/questions/20131553/angularjs-convert-dates-in-controller) – lin Mar 01 '17 at 10:33
  • Go through https://docs.angularjs.org/api/ng/filter/date#examples at least once – Sudhir Kaushik May 21 '19 at 06:32

2 Answers2

1

If you want to convert that variable in HTML,

{{variable | date : 'yyyy-MM-dd HH:mm:ss'}}

And in case of JS,

$filter('date')(variable, 'yyyy-MM-dd HH:mm:ss')

Note: In case of JS, do not forget to add $filter as dependency.

Daxil Shah
  • 26
  • 2
0

You can format date in AngularJS using the pipe operator and the date filter

<span>{{1488195848000| date:'yyyy-MM-dd HH:mm:ss'}}</span>

In the controller you need to make use of filter function to format the date like

var _date = $filter('date')(_date, 'yyyy-MM-dd HH:mm:ss')
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400