JSFiddle - React, Tailwind, and code Playground

by lbstr

HTML

<div id="log"></div>

JavaScript

var isValidDate = function(s){
    if (!s) { return false; }
    var date = new Date(s),
        regex = /(\d+)\/(\d+)\/(\d+)/,
        matches = regex.exec(s),
        m, d, y;
    
    if (isNaN(date.getTime())) {
        return false;
    }
    
    if (!matches || matches.length !== 4) {
        return false;
    }
    
    m = +matches[1];
    d = +matches[2];
    y = +matches[3];
    
    return d === date.getDate() && m === date.getMonth() - 1 && y === date.getYear();
};

var logDate = function(date) {
    var $log = log.$log || (log.$log = document.getElementById('log')),
        m = document.createElement('div');
    
    m.innerHTML = date + ": " + (isValidDate(date) ? "valid" : "invalid");    
    $log.appendChild(m);
};

logDate("1/1/2013");