'Mongo DB convert string to dates

I have a string date like this 'Sat Apr 02 2022 16:36:53 GMT+0530 (India Standard Time)' and i want to convert this string to an ISO date. My main goal is to compare this string with an input date string in the Mongo DB database.

Is there any way to do so?

I have tried using the $toDate operator but I always get this error PlanExecutor

error during aggregation :: caused by :: Error parsing date string 'Sat Nov 06 2021 19:46:08 GMT+0000 (Coordinated Universal Time)'; 41: Double timezone specification 'n'; 47: Double timezone specification 'U'; 53: Double timezone specification 's'; 57: Double timezone specification 'T'; 34: Double timezone specification '('

Is there any way to do so?



Solution 1:[1]

(new Date()).toISOString()

Converts your date object to ISO String. You can pass a dateString to it, achieving something of the like of

(new Date(dateString)).toISOString()

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Lajos Arpad