mantis-toboggan
mantis-toboggan

Reputation: 1

Using moment.js to determine if moment() is before, during or after a specific time block

I'm having trouble wrapping my head around moment.js. Essentially what I want to do is determine if the current time is before, between or after a certain time block. I can't figure out if I should use an array for my specific time blocks, or an object, or maybe I'm going about this all wrong... Any guidance would be awesome.

Upvotes: 0

Views: 2353

Answers (1)

MEDZ
MEDZ

Reputation: 2295

You can pass the format of your input to moment js and then you can compare them:

let momentTime = moment("13:30", 'HH:mm');
let laterMomentTime = moment("15:00", 'HH:mm');

if(momentTime.isBefore(laterMomentTime)){
  console.log("Yes 1:30 pm is earlier");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

Upvotes: 2

Related Questions