0

Am working on a functionality where I need to display timestamps stored in mongodb in dd-mm-yyyy format. But am confused on how to write the query to get the timestamps as in required format.

Below is the sample data of dropdowns collection.

/* 1 */
{
    "_id" : ObjectId("5e787e988f3b3f3a240e846e"),
    "status" : true,
    "company" : ObjectId("5e787e988f3b3f3a240e846d"),
    "type" : "ticketstatus",
    "name" : "Open",
    "createdAt" : ISODate("2020-03-23T09:17:12.374Z"),
    "updatedAt" : ISODate("2020-03-23T09:17:12.374Z")
}

/* 2 */
{
    "_id" : ObjectId("5e787e988f3b3f3a240e846f"),
    "status" : true,
    "company" : ObjectId("5e787e988f3b3f3a240e846d"),
    "type" : "ticketstatus",
    "name" : "Closed",
    "createdAt" : ISODate("2020-03-23T09:17:12.374Z"),
    "updatedAt" : ISODate("2020-03-23T09:17:12.374Z")
}

/* 3 */
{
    "_id" : ObjectId("5e7883ce8f3b3f3a240e8472"),
    "status" : true,
    "company" : ObjectId("5e787e988f3b3f3a240e846d"),
    "type" : "ticketpriorities",
    "name" : "High",
    "createdAt" : ISODate("2020-03-23T09:39:26.167Z"),
    "updatedAt" : ISODate("2020-03-23T09:39:26.167Z")
}

The query am using so far,

dropdowns.find({"company":ObjectId('5e787e988f3b3f3a240e846d')});

What would be the right approach to fetch the dates? Do I need to write a query or do I need to manipulate the returned data from the query using the lines of code? Please suggest me the best and most used way. Thanks.

Aravind
  • 424
  • 3
  • 19
  • 1
    Hey Aravind, Check this answer. I think this answers to similar problem. https://stackoverflow.com/questions/43591405/format-date-in-mongodb-query-output-on-shell – saiteja Apr 16 '20 at 05:27
  • Thanks, @saiteja . This is really helpful – Aravind Apr 16 '20 at 11:14

1 Answers1

1

You should format your dates on the client-side using a library like moment or date-fns.

For example, with date-fns:

import { format } from 'date-fns'

format(new Date(), "dd-MM-yyyy")
// 16-04-2020

date-fns is the library of choice these days because it is modular, meaning you can import only the functions you need (no affiliation). Be sure to also consider your end-user's time zone.

If you want to do it on the server-side, use $dateToString with aggregation as mentioned in the comment above:

db.collection.aggregate([
  {
    "$match": {
      company: ObjectId("5e787e988f3b3f3a240e846d")
    }
  },
  {
    "$project": {
      createdAt: {
        $dateToString: {
          format: "%d-%m-%Y",
          date: "$createdAt"
        }
      },
      updatedAt: {
        $dateToString: {
          format: "%d-%m-%Y",
          date: "$updatedAt"
        }
      },

    }
  }
])

Working example here.

dikuw
  • 1,134
  • 13
  • 22
  • Thanks for the help. But can't I directly get them in required format from database itself? I mean, is there any chance to define date format in query? – Aravind Apr 16 '20 at 11:13
  • @dikuw, How Can I change timestamps createdAt and updatedAt format is like (YYYY-MM-DD HH:mm:ss) – Ravindra S. Patil Jun 25 '21 at 07:11