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')}}
2
  • @barbsan unfortunately the same result, nothing showed Commented Mar 21, 2018 at 13:59
  • Possible duplicate of Using AngularJS date filter with UTC date Commented Mar 21, 2018 at 15:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

this is perfect! but I don't know why it doesn't show the year and it shows YYYY instead :D
@dadsa might depend on the version, try just Y
@AlekseySolovey, now it shows that single Y. you're talking about AngularJS version? it's 1.6
@dadsa typo, just y *
@dadsa you can check this link for more info about the syntax: docs.angularjs.org/api/ng/filter/date
|
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.