1

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?

4
  • Are you sure? When you say "comes from a MongoDB object" then it seems to be already a Date object and you don't need to convert it. "Wed Dec 28 2022 15:00:00 GMT+0100 (hora estándar de Europa central)" is just the way how your terminal displays it. Commented Jan 2, 2023 at 11:52
  • Hi @Wernfried Domscheit. I don't understand why, but despite being this data saved as a Date in the model, executing console.log(typeof date2) results on Object Commented Jan 3, 2023 at 8:25
  • Then it is a date. typeof new Date() also returns Object Commented Jan 3, 2023 at 8:32
  • Oh, Okay. Didn't know, thanks for the explanation! I solved it using moments as explained below Commented Jan 3, 2023 at 8:53

3 Answers 3

1

Based on the information the value from MongoDB (here: date2) is not a string, it is a Date object. Thus you need to convert only the value from Python (here: date1):

if ( moment(date1, 'DD/MM/YY HH:mm').toDate() > date2 ) {
   console.log('true')
} else {
   console.log('false')
}

Or, if you prefer to work with Moment objects:

moment(date1, 'DD/MM/YY HH:mm').isAfter(moment(date2))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time to explain it!
1

You can use momentjs to format dates in javascript. He is an Example:

let date1 = "25/12/22 15:00"
let date2 = "Wed Dec 27 2022 15:00:00 GMT+0100"

if(moment(date1,'DD/MM/YY HH:mm') > new Date(date2)){
  console.log('True')
} else {
    console.log('false')
}
<script type = "text/JavaScript" src = " https://MomentJS.com/downloads/moment.js"></script>

4 Comments

it is not showing true value for date1 = 27/12/22
I think for moment.js you should better use isAfter/isBefore
@Sammed In case date1 = 27/12/22 as it is same date as date2 I guess you should use the operator >= instead of > at if condition if you want a True in this situation
still not working for me
1

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.

function convert(mongoDateString) {
          const monthAr = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
          const mongoDateAr = mongoDateString.split(" ");
          const dayNo = mongoDateAr[2];
          const monthNo = monthAr.indexOf(mongoDateAr[1]);
          const yearNo = mongoDateAr[3];
          const time = mongoDateAr[4].substring(0, mongoDateAr[4].length - 3);
          return dayNo + "/" + monthNo + "/" + yearNo + " " + time;
        }

        let thisDateString = "Wed Dec 28 2022 15:00:00 GMT+0100 (hora estándar de Europa central)"
        console.log(convert(thisDateString ))

3 Comments

Why do you reinvent the wheel? There are many ready-to-use libraries (e.g. Moment.js, Luxon, Day.js) which do this in a single line.
Hi, thanks for the answer! Seems like doing it with moments it's pretty easier, take a look at the answer if you can. Anyways, thanks for taking your time searching for a solution
Sure there are many libraries, with varying overhead, but I thought the original poster might like to see how date (and other) strings can be manipulated with splitting, as this was something he appeared not to be aware of. When I started, I found this more educative than plugging in a library. But I understand your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.