Rupak Banerjee
Rupak Banerjee

Reputation: 135

Date conversion in Mongo Db

I am having trouble converting "7/26/2020 18:34:25" into any date format from which Mongo can run aggregate operation.

The date in the Mongo is stored as string. I have tried "$toDate", "$convert" and "$dateToString".

Upvotes: 0

Views: 97

Answers (2)

Hamza Khurshid
Hamza Khurshid

Reputation: 21

Try using Moment Library for converting you date In your case this code can help you:

moment('7/26/2020 18:34:25').toDate();

You can test this code on browser side on this page enter link description here

Upvotes: 0

Gibbs
Gibbs

Reputation: 22974

You need to use $dateFromString

$project: {
      date: {
         $dateFromString: {
            dateString: '$dateFieldName',
            format: '%m/%d/%Y %H:%M:%S'
         }
      }
   }

playground

Upvotes: 2

Related Questions