Search code examples
angulartypescriptspring-bootdatezoneddatetime

How to compare ZonedDateTime (Spring Boot) with Date (TypeScript)?


My Spring Data REST returns a ZonedDateTime date format like 2022-04-29T00:00:00+02:00 and in TypeScript (Angular) I have dates as Sat Apr 23 2022 00:00:00 GMT+0200.

My goal is to compare two date converting the ZonedDateTime to the required format in Angular. How to do that?


Solution

  • You can use both Strings as you already have them to create the Date object, using the constructor. The constructor understands both forms.

    let val1 : Date = new Date('2022-04-29T00:00:00+02:00');
    let val2 : Date = new Date('Fri Apr 29 2022 00:00:00 GMT+0200');
    

    Then you can compare both objects using the getTime() method which returns an absolute representation in miliseconds

    The getTime() method returns the number of milliseconds since the ECMAScript epoch.

    if (val1.getTime() === val2.getTime()){
      console.log('true');
    }