0

I am trying to retrieve time data from MongoDB using AngularJS.
The tricky part is that I am using MongoJS without having a schema for my time data thus I cannot query it directly. Whenever I pull data from my database, I receive it in a format of:

2017-06-29T23:00:00.000Z

my main issue is that I don't use schema to set up a specific format of the data and I want it to display in:

dd/mm/yyyy

This is my NodeJS code used for the overall data input, get, delete and edit:

app.post('/posts', function(req,res){
    dbc.posts.insert(req.body, function(err, doc){
        res.json(doc);
    })
});

app.delete('/posts/:id', function(req,res){
    var id = req.params.id;
    console.log(id);
    dbc.posts.remove({_id: mongojs.ObjectId(id)}, function(err, doc){
        res.json(doc);
    })
});

app.get('/posts/:id', function(req,res){
    var id = req.params.id;
    console.log(id);
    dbc.posts.findOne({_id: mongojs.ObjectId(id)}, function(err, doc){
        res.json(doc);
    });
});

app.put('/posts/:id', function(req, res){
   var id = req.params.id;
    console.log(req.body.system);
    dbc.posts.findAndModify({query:{_id:mongojs.ObjectId(id)},
        update: {$set: {issue: req.body.issue, system: req.body.system, date: req.body.date, priority: req.body.priority, description: req.body.description, status: req.body.status}},
          new:true}, function(err, doc){
        res.json(doc);
        console.log(req.body.date);
    });
});

How can I filter my data in my AngularJS controller to display it in the format I want, since I don't use a specific schema for the database?

2 Answers 2

1

There is a time filter that angularjs provides by defualt. You can use it directly

 {{ date_expression | date : format : timezone}}

Read the docs here

You can change this to suit your need.

{{date_expression| date:'dd MMM, yyyy '}}
Sign up to request clarification or add additional context in comments.

1 Comment

This works in general but it displays the data like " Jun 30, 2017 " and I want it " 30 Jun, 2017". That is what I am trying to achieve.
0

MomentJs is always batter option if we talk about date formatting or date conversion.

so in controller you need to create a helper function like:

function getFormateDate(date){
  moment(date).format("DD/MM/YYYY")
}

and don't forget to add this script in index.html

<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>

For more details you can check : https://momentjs.com/

2 Comments

Momentjs is a good option but only when you have complex date calculations. If it's only about date formatting, it comes in-built with angular.
no its not about complex date calculation. we can use in both case. if you want to save your time so I'd suggest moment. in feature might be you need to do some date multiplication and all so you should use moment.

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.