JSFiddle - React, Tailwind, and code Playground
JavaScript
function timeCompare(d, base_d) {
/* fn timeCompare : compares two dates, both of which can be passed in one of two ways.
* d: (Date or String) the Date to be tested - a js Date object or a timeString h:m[:s[:ms]].
* base_d: (Date or String) (optional) the base Date against which d is tested - a js Date object or a timeString h:m[:s[:ms]].
* d and base_d both default to time NOW if missing or invalid.
* Returns a js object with the following properties:
* - d: the original d converted where necessary from timestring to a js Date object
* - base_d: the original base_d converted where necessary from timestring to a js Date object
* - before: (boolean) true if d < base_d, otherwise false.
* - after: (boolean) true if d > baseDate, otherwise false.
* - equalTo: (boolean) true if d == baseDate, otherwise false.
*/
function throwError(message) {
//Throws a non-fatal js error
setTimeout(function() {
throw (message);
}, 0);
}
function time2Date(timeStr) {
//converts (String) hh:mm[:ss[:ms]] to milliseconds since midnight
var tArr = (timeStr.split) ? timeStr.split(':') : [];
if (tArr.length < 2) {
throwError('fn timeIsBefore(): argument is not in the format hh:mm, hh:mm, hh:mm:ss or hh:mm:ss:ms');
}
tArr[0] = Number(tArr[0]);//hours
tArr[1] = Number(tArr[1]);//minutes
tArr[2] = Number(tArr[2]);//seconds
tArr[3] = Number(tArr[3]);//milliseconds
if(isNaN(tArr[0]) || isNaN(tArr[1]) || isNaN(tArr[2]) || isNaN(tArr[3])){
throwError('Warning: fn timeIsBefore('+timeStr+'): hours, minutes, seconds or milliseconds is not a number');
}
var d = new Date();
d.setHours(tArr[0] || 0);//default missing or invalid hours to zero
d.setMinutes(tArr[1] || 0);//default missing or invalid mins to zero
d.setSeconds(tArr[2] || 0);//default missing or...