0

Accepted time should be like, Basically, it should be either 00 or 30.

9:00 AM

Not like :

12:34 AM

I can think of regular exp. Is there any other way ? I can use moment.js. Any suggestions ?

vikas95prasad
  • 1,234
  • 1
  • 12
  • 37
  • 1
    Does this answer your question? [Round moment.js object time to nearest 30 minute interval](https://stackoverflow.com/questions/25323823/round-moment-js-object-time-to-nearest-30-minute-interval) – shrys Jan 13 '20 at 09:30
  • @shrys No, I just need a condition to find out. – vikas95prasad Jan 13 '20 at 09:32

3 Answers3

1

You could use the following regex to test your string:

/^([0-1]?[0-9]|2[0-3]):[0|3][0]\s[A|P]M$/

Sample

const r = /^([0-1]?[0-9]|2[0-3]):[0|3][0]\s[A|P]M$/;

['9:00 AM',
  '12:30 PM',
  '3:30 PM',
  '5:00 PM',
  '12:30 PM',
  '12:34 AM',
  '3:21 PM',
  '4:56 AM'
].forEach(a => console.log(a, r.test(a)));

shrys
  • 5,860
  • 2
  • 21
  • 36
0

Use moment, you can use like:

const momentTime = moment(yourTime)
const minute = momentTime.minute()

if (minute === 0 || minute === 30) { // do your things }

But I think @shrys answer using regex maybe better.

tomision
  • 964
  • 9
  • 22
0

For each input like this, you can parse it with moment.js and query the minutes:

let times = [
    '9:00 AM',
    '12:30 PM',
    '3:30 PM',
    '5:00 PM',
    '12:30 PM',
    '12:34 AM',
    '3:21 PM',
    '4:56 AM',
];

times.map(time => {
    let parsedTime = moment(time, 'hh:mm');
    let minutes = parsedTime.minutes();

    if (minutes === 0 || minutes === 30) {
        // do stuff
    }
});
Kamen Minkov
  • 3,324
  • 1
  • 14
  • 21