Ilia Tapia
Ilia Tapia

Reputation: 649

Find date difference in hours?

I have a case where I need to check the diff of the hours between two Date-time. I am new in typescript but in java, I have done something like below just a small example to show you what I need.

  int hours = (int) ChronoUnit.HOURS.between(app.getDisableAt(), LocalDateTime.now(Clock.systemUTC()));

And I need the same thing but in front-end. By this, I get the hour's diff correctly, but I don't want to save in database so I prefer the front-end.

Upvotes: 1

Views: 150

Answers (1)

Chaitanya
Chaitanya

Reputation: 939

Convert into epoch date and get difference between two dates and divide by 1000*3600

getDateDiffInHours(date1: Date, date2: Date) {
        return Math.floor((new Date(date2).getTime() - new Date(date1).getTime()) / 3600000);
      }

Upvotes: 1

Related Questions