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 ?
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 ?
You could use the following regex to test your string:
/^([0-1]?[0-9]|2[0-3]):[0|3][0]\s[A|P]M$/
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)));
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.
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
}
});