'Formate date, and sort data by date in MongoDb, NodeJs

Looking for mongoose query and formation of date

I have this date formation as a input Date : [Wed Apr 28 2022 15:21:09 GMT-0400 (Eastern Daylight Time), Sat Apr 30 2022 15:21:09 GMT-0400 (Eastern Daylight Time)]

sort data by give Date

Date : [Wed Apr 28 2022 15:21:09 GMT-0400 (Eastern Daylight Time), Sat Apr 30 2022 15:21:09 GMT-0400 (Eastern Daylight Time)]

[
 {
   itemName: a
   createdAt: 2022-04-29T05:57:41.238+00:00
 },
 {
   itemName: b
   createdAt: 2022-04-30T05:57:41.238+00:00
 },
 {
   itemName: c
   createdAt: 2022-05-30T05:57:41.238+00:00 // this date is out of range
 }
]

Answer 
{
 itemName: a,
 itemName: b
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source