0

I have for example that time "15:00:00" and "15:30:00"

firstly i do split and cast to Integer

const hour = Number(this.time.split(':')[0]);
const minutes = Number(this.time.split(':')[1]);

and later i using for example if(hour > hour1) { .... }

I know that this method is not optimal is some tips to compare time?

Wojciech Abacki
  • 251
  • 1
  • 4
  • 17
  • how are you comparing time of two different date, like, let say, one is `23:00:00 ` and the other one is `01:00:00` of the next day – McRist Aug 20 '18 at 16:39
  • you can convert the time string to a common format (like seconds) and then compare them – Sid Vishnoi Aug 20 '18 at 16:40
  • 3
    Possible duplicate of [How can I compare two time strings in the format HH:MM:SS?](https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss) – Ivar Aug 20 '18 at 16:43
  • 1
    `'15:00:00' > '15:30:00'` – Ivar Aug 20 '18 at 16:45
  • Plz check this: https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss – dj079 Aug 20 '18 at 16:48

1 Answers1

1

If you can use libs, I'd recommend moment.js. It makes it easy to work with datetimes.

For example:

var first = moment('15:00:00', 'hh:mm:ss');
var second = moment('15:30:00', 'hh:mm:ss');
console.log(first.isAfter(second));
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
Phiter
  • 14,570
  • 14
  • 50
  • 84