Search code examples
javascriptangularjsmomentjsangular-moment

Javascript date string to angular ng-moment variable


I have a javascript string in the following format:

2016-06-22T14:47:29.689358

How would I use ng-moment to parse the string into a moment object and then format it inside my view?

https://github.com/urish/angular-moment

Pseudocode:

$scope.time = "2016-06-22T14:47:29.689358";
<span am-time-ago="time | amParse:'YYYY.MM.DD HH:mm:ss'"></span>

It seems like time needs to be converted into a Date object before it is passed to ng-moment.


Solution

  • You don't need a date at all, and in fact you shouldn't use the date object's parser as it behaves in odd ways. You just have the wrong format specified for the date you have.

    <span am-time-ago="time | amParse:'YYYY-MM-DDTHH:mm:ss.SSS'"></span>
    

    That should be all you need.

    For more information about why dates parse unreliably, you can see this question.