Ravi Teja Muddada
Ravi Teja Muddada

Reputation: 391

Mongo Query to convert "EEE, d MMM yyyy HH:mm:ss Z" to YYYY-mm-dd

I need to convert a date to a format "Mon Oct 17 09:00:00 IST 2016" to 2016-10-17

my query is like this:

db.collection.aggregate( [ { $project: { yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date:new Date("Mon Oct 17 09:00:00 IST 2016") } } } }] )

How can I achieve that through a mongo query Kindly help me.

Upvotes: 0

Views: 833

Answers (1)

IftekharDani
IftekharDani

Reputation: 3729

Please use "dateToString" in aggregate also check Link

Ex: $date is Your Date to Convert.

db.Collection.aggregate(
   [
     {
       $project: {
          yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date: "$date" } }
       }
     }
   ]
)

Upvotes: 1

Related Questions