so I ran into this little problem and I don’t know how to solve it. I have two dates, one that comes from a Python script that arrives as a String, and one that comes from a MongoDB object. I need to compare them but the date coming from MongoDB seems to have a strange format that doesn’t allow me to do so. This is an example of the dates formats:
String: 29/12/22 15:00
Object: Wed Dec 28 2022 15:00:00 GMT+0100 (hora estándar de Europa central)
From this I can easily transform the String coming from Python to a Date object, but what should I do with the MongoDB object to be able to compare them?
3
Answers
You can use momentjs to format dates in javascript.
He is an Example:
Here’s a very longhand method: pass the mongo date as a string, split it into its components, and rebuild it. Here, it’s rebuilt to the same format as your Python script produces.
Based on the information the value from MongoDB (here:
date2
) is not a string, it is aDate
object. Thus you need to convert only the value from Python (here:date1
):Or, if you prefer to work with
Moment
objects: