All files / src/utils/common/dates isDate.ts

0% Statements 0/9
0% Branches 0/4
0% Functions 0/1
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                                           
/**
 * Checks if the input string is a valid date string in formats like YYYY-MM-DD, YYYY/MM/DD, etc.
 *
 * @param {string} date - The string to be checked for date validity.
 * @return {boolean} Returns true if the input string is a valid date, otherwise false.
 */
export const isDate = (date: string): boolean => {
  Iif (date.length < 10) return false;
 
  // Regular expression to match date formats like YYYY-MM-DD, YYYY/MM/DD, etc.
  const datePattern =
    /^\d{4}[-/]\d{2}[-/]\d{2}([ T]\d{2}:\d{2}:\d{2}(\.\d{1,6})?)?$/;
 
  // Check if the string matches the date pattern
  Iif (!datePattern.test(date)) {
    return false;
  }
 
  const d = new Date(date);
  return d instanceof Date && !isNaN(d.valueOf());
};