JSFiddle - React, Tailwind, and code Playground

by Nadeesh Peiris

JavaScript

const isValidDate = (date) => date instanceof Date && !Number.isNaN(date.getTime());

const getMonthValue = (month) => {
  let monthValue = 12;
  if (month === 'JAN') {
    monthValue = 0;
  } else if (month === 'FEB') {
    monthValue = 1;
  } else if (month === 'MAR') {
    monthValue = 2;
  } else if (month === 'APR') {
    monthValue = 3;
  } else if (month === 'MAY') {
    monthValue = 4;
  } else if (month === 'JUN') {
    monthValue = 5;
  } else if (month === 'JUL') {
    monthValue = 6;
  } else if (month === 'AUG') {
    monthValue = 7;
  } else if (month === 'SEP') {
    monthValue = 8;
  } else if (month === 'OCT') {
    monthValue = 9;
  } else if (month === 'NOV') {
    monthValue = 10;
  } else if (month === 'DEC') {
    monthValue = 11;
  }
  return monthValue;
};

const convertToDate = (date) => {
  const splittedDate = date.split('-');
  return new Date(parseInt(splittedDate[2], 10), parseInt(getMonthValue(splittedDate[1]), 10), parseInt(splittedDate[0], 10));
};

const initialDate = convertToDate("a");

const result = isValidDate(initialDate);

alert(initialDate+" "+result);