1

I'm New to Node.Js and MongoDB . Am getting Date as CreatedDate : 2018-09-06T06:14:53.111Z . In output am trying to display as "MM/DD/YYYY" .

Here is my code am getting last 60 days data form Collection.

 var today = new Date();

    today.setDate(today.getDate() - 60);

    ODSCase.find({ UserGUID: req.UserGUID ,CreatedDate:{"$gte":new Date(today)} }).sort({ModifiedDate: -1})
   .exec()
   .then(doc => {
       res.status(200).json(doc);
   })
   .catch(err =>{ err500.err500(err,req,res,next);});

Output:

 {

    "_id": "5b9242aed7ae250c74325362",
    "CustomerGUID": "ee1ac5c7-c0f9-466c-8859-95dc1fcd0334",
    "InstanceGUID": "878aee54-1b95-46d1-9e26-b9eaea9f9cd4",
    "UserGUID": "f0272467-3ec5-48f7-5553-987900b57a11",
    "CaseNumber": 1536311981016,
    "CaseStatus": "New",
    "CampaignID": "66336533-3765-3266-4d43-382f392f3230",
    "CreatedDate": "2018-09-07T09:19:42.586Z",
    "CreatedBy": "f0272467-3ec5-48f7-5553-987900b57a11",
    "ModifiedDate": "2018-09-07T09:19:42.586Z",
    "ModifiedBy": "f0272467-3ec5-48f7-5553-987900b57a11",
    "IsDeleted": false,
    "Source": "Dev",
    "__v": 0
},
1
  • With a ODM as mongoose you can override the method toObject to give format to some attributes. Another option is before then use map and give format with some module as momentjs.com Commented Sep 10, 2018 at 9:20

2 Answers 2

1

Consider running an aggregate pipeline instead which allows you to use operators such as $dateToString that can transform the date to the desired format.

For example, the same query above can be run using the aggregation framework as:

let today = new Date();
today.setDate(today.getDate() - 60);

const date = new Date(today);

ODSCase.aggregate([
    { '$match': { 
        'UserGUID': req.UserGUID,
        'CreatedDate': { '$gte': date } 
    } },
    { '$sort': { 'ModifiedDate': -1 } },
    { '$addFields': {
        'CreatedDate': { 
            '$dateToString': { 
                'format': '%m/%d/%Y', 
                'date': '$CreatedDate' 
            }
        }
    } } 
])
.exec()
.then(doc => {
    res.status(200).json(doc);
})
.catch(err => err500.err500(err, req, res, next) );

You can also use the excellent Moment.js JavaScript date library to create the date object used in the query:

const moment = require('moment');
const date = moment().subtract(60, 'days').toDate(); // or...
const date = moment().add(-60, 'days').toDate();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi chridam , Am getting "Error": "The aggregation pipeline is not enabled for this account. Please see aka.ms/mongodb-aggregation for details." . Am trying to set aggregate enable in Azure Portal .. I will try this and Let you Know . Thank you
0

I am assuming you want to deal only with the date format in output which is currently in ISO string to make it in the form of mm/dd/yyyy, you can try this:

var today = new Date();

    today.setDate(today.getDate() - 60);

    ODSCase.find({ UserGUID: req.UserGUID ,CreatedDate:{"$gte":new Date(today)} }).sort({ModifiedDate: -1})
   .exec()
   .then(doc => {
       doc.CreatedDate = new Date(doc.CreatedDate).toLocaleDateString('en-US');
       res.status(200).json(doc);
   })
   .catch(err =>{ err500.err500(err,req,res,next);});

2 Comments

Hi Ashish , No change in Output : "CreatedDate": "2018-09-07T09:19:42.586Z", with your answer
Edited. There was a typo, apologies.

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.