0

Having a timestamp, for example 1519357500, is it possible to send it in this form to html and convert it into date format inside interpolation?

I've tried to do it like this but it doesn't work:

{{moment($ctrl.myTimestamp).format('MMMM Do YYYY, h:mm:ss a')}}
dadsa
  • 177
  • 1
  • 13
  • @barbsan unfortunately the same result, nothing showed – dadsa Mar 21 '18 at 13:59
  • Possible duplicate of [Using AngularJS date filter with UTC date](https://stackoverflow.com/questions/20662140/using-angularjs-date-filter-with-utc-date) – Kyle Krzeski Mar 21 '18 at 15:03

2 Answers2

2

Yes, very easily.

{{$ctrl.myTimestamp | date:'MMMM d y, h:mm:ss a'}}

(this assumes $ctrl.myTimestamp contains the epoch milliseconds)

If you have the seconds till epoch do this:

{{$ctrl.myTimestamp * 1000 | date:'MMMM d y, h:mm:ss a'}}

More information here.

Giovani Vercauteren
  • 1,898
  • 13
  • 22
0

This is how I would have done it.

ps. I am just doing it using no dependency (moment). coz I've never used it. So this is how you can achieve this.

var app = angular.module('test-app', []);
app.controller('testCtrl', function($scope){

$scope.timestamp = "1519357500";
var date = new Date($scope.timestamp * 1000);
var datevalues = ('0' + date.getDate()).slice(-2) + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + date.getFullYear() + ' ' + date.getHours() + ':' + date.getMinutes();
$scope.timestamp = datevalues;
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test-app" ng-controller="testCtrl">
 {{timestamp}}
</div>
Muhammad Usman
  • 10,039
  • 22
  • 39