1

I have question about date format in mongoDB, express and finally AngularJS. I tried to save user comment in mongoDB and next show in view. With comment body and comment author I have no problem but with comment date is. I save date using new Date(), and in database I have usual date format. And now starts my problem, how display this date in more clear format e.g. yy/mm/dd (best solution will be 2 minutes ago or 1 year ago). I tried use typical angular format date but this no working. All time I get this format: 2017-09-05T12:13:08.611Z. Look on my extract from code:

api (save in database)

product.comments.push({
   body: req.body.comment,
   author: user.username,
   date: new Date(),   
 });

controller

.controller('seeMoreCtrl', function($scope, $routeParams, User, $location, $window){ 

var app = this;

User.readMoreCourse($routeParams.id).then(function(data){
    if(data.data.success){
        app.comments = data.data.product.comments; 
    } else {
        $window.location.assign('/register');
    }
});
})

html

<div ng-repeat="comment in seeMore.comments" class="all-users-comment-course">
   <article class="one-comment">
     <p class="user-name">{{ comment.author }}</p>
     <p class="comment-date">{{ comment.date | date }}</p> 
   </article>
</div>

service

userFactory.readMoreCourse = function(id) {
    return $http.get('/api/details/' + id) 
}

schema

comments: [{
    body: {
        type: String,
        trim: true,
    },
    author: {
        type: String,
    },
    date: {
        type: String
    }
}]
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
Mat.Now
  • 1,715
  • 3
  • 16
  • 31
  • 1
    I think this is what you need to look at:- https://stackoverflow.com/questions/33109897/get-distinct-iso-dates-by-days-months-year – Asim Sep 05 '17 at 12:27
  • Alternatively, there is [moment.js](https://momentjs.com/), although for what you are trying to do you might need a [moment.js pipe](https://github.com/urish/angular2-moment). – Gabriel Lovetro Sep 05 '17 at 12:42

0 Answers0