I wrote the following JS function to check if a date comes in the right format and can be used further down in the application
const getInvalidDay = day => {
const date = new Date(day);
const dateToMoments = [date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()];
return dateToMoments.some(moment => moment !== 0);
};
An example snippet to see how it works
const getInvalidDay = day => {
const date = new Date(day);
const dateToMoments = [date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds()];
return dateToMoments.some(moment => moment !== 0);
};
console.log('Should be true - not valid::: ', getInvalidDay('2021-03-16T09:00:00.000Z'));
console.log('Should be false - is valid::: ', getInvalidDay('2021-03-16'));
When we have all moments as 0
means the date is valid so we return false
instead, if some have !==0
then it is true
and the date is not valid
I was wondering if there is another way to achieve the same result