Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
<head>
    <title>Validate date and time</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/5.2.0/mocha.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.1.2/chai.min.js"></script>
</head>
<body>
<!-- The element display test result -->
<div id="mocha"></div>
<script type="text/javascript">
    /**
     * Check if string is empty
     * @param value
     * @returns {boolean}
     */
    function isEmpty(value) {
        return value === '' || value === undefined ||
            value == null || value.length === 0 || /^\s+$/.test(value);
    }

    /**
     * Check time 24h format between 00:00 and 23:59
     * @param value
     * @returns {boolean}
     */
    function checkTime24h(value) {
        return value && /^([01]\d|2[0-3])(:[0-5]\d){0,2}$/.test(value);
    }

    /**
     * Check date valid in dd/mm/yyyy format and valid in value
     * @param value
     * @returns {boolean}
     */
    function checkDateFormat(value) {
        var regex, date;
        regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;

        if (isEmpty(value) || !regex.test(value)) {
            return false;
        }

        if (!isValidDate(value)) {
            return false;
        }

        date = new Date(value.replace(regex, '$2/$1/$3'));

        return parseInt(RegExp.$2, 10) === 1 + date.getMonth() &&
            parseInt(RegExp.$1, 10) === date.getDate() &&
            parseInt(RegExp.$3, 10) === date.getFullYear();
    }

    /**
     * Check value is a valid date
     * @param dateString format dd/mm/yyyy
     * @returns {boolean}
     */
    function isValidDate(dateString) {
        var dateData = dateString.split('/');
        var year = dateData[2];
        var month = dateData[1];
        var day = dateData[0];
        var date = new Date(year, month - 1, day);

        return (date.getFullYear() == year) && (date.getMonth() == month - 1) && (date.getDate() == day);
    }

    /**
     * Check datetime from-to range valid
     * @param dateFrom
     * @param dateTo
     */
    function checkDateTimeRange(dateFrom, dateTo) {
        var fromValue = new Date(dateFrom);
        var toValue = new Date(dateTo);

        return fromValue.getTime() <= toValue.getTime();
    }
</script>
<script type="text/javascript">
    mocha.setup("bdd");
    var expect = chai.expect;

    var time24hTestCases = [
        {input: 'abcd', output: false},
        {input: '1234', output: false},
        {input: '1:35', output: false},
        {input: '01:5', output: false},
        {input: '00:00', output: true},
        {input: '00:59', output: true},
        {input: '10:30', output: true},
        {input: '10:75', output: false},
        {input: '12:00', output: true},
        {input: '12:59', output: true},
        {input: '00:60', output: false},
        {input: '13:59', output: true},
        {input: '15:00', output: true},
        {input: '21:35', output: true},
        {input: '23:59', output: true},
        {input: '24:00', output: false},
        {input: '25:00', output: false}
    ];

    var dateTestCases = [
        {input: 'abcdefghij', output: false},
        {input: '0123456789', output: false},
        {input: 'ab/12/2018', output: false},
        {input: '00/00/0000', output: false},
        {input: '00/00/2018', output: false},
        {input: '00/01/2018', output: false},
        {input: '01/01/2018', output: true},
        {input: '31/01/2018', output: true},
        {input: '32/01/2018', output: false},
        {input: '28/02/2018', output: true},
        {input: '30/02/2018', output: false},
        {input: '29/02/2018', output: false},
        {input: '29/02/2000', output: true},
        {input: '9/02/2000', output: false},
        {input: '30/06/2000', output: true},
        {input: '31/06/2000', output: false},
        {input: '30/6/2000', output: false},
        {input: '15/03/2018', output: true},
        {input: '15/03/20ab', output: false},
        {input: '15/03/ 2018', output: false},
        {input: 'ab/03/2018', output: false},
        {input: '15-03-2018', output: false},
        {input: '15/ab/2018', output: false},
        {input: '31/04/2018', output: false},
        {input: '15/13/2018', output: false}
    ];

    var dateRangeTestCases = [
        {from: '01/09/2018', to: '01/09/2018', output: true},
        {from: '01/09/2018', to: '02/09/2018', output: true},
        {from: '01/09/2018', to: '01/10/2018', output: true},
        {from: '01/09/2018', to: '31/08/2018', output: false},
        {from: '02/09/2018', to: '01/09/2018', output: false},
        {from: '01/09/2018 23:59', to: '01/09/2018', output: false},
        {from: '01/09/2018 00:00', to: '01/09/2018', output: true},
        {from: '01/09/2018', to: '01/09/2018 01:00', output: true},
        {from: '01/09/2018 00:00', to: '01/09/2018 00:00', output: true},
        {from: '01/09/2018 00:00', to: '01/10/2018 00:00', output: true},
        {from: '01/09/2018 00:00', to: '01/09/2018 00:01', output: true},
        {from: '01/09/2018 01:00', to: '01/09/2018 02:00', output: true},
        {from: '01/09/2018 01:00', to: '01/09/2018 00:01', output: false},
        {from: '01/09/2018 01:00', to: '31/08/2018 01:00', output: false}
    ];

    describe('Validate time 24h', function () {
        time24hTestCases.forEach(function (test) {
            it(test.input + ' is ' + test.output, function () {
                expect(test.output).to.equal(checkTime24h(test.input));
            })
        });
    });

    describe('Check valid datetime', function () {
        dateTestCases.forEach(function (test) {
            it(test.input + ' is ' + test.output, function () {
                expect(test.output).to.equal(checkDateFormat(test.input));
            })
        });
    });

    describe('Check valid datetime range from to', function () {
        dateRangeTestCases.forEach(function (test) {
            it('from ' + test.from + ' to ' + test.to + ' is ' + test.output, function () {
                expect(test.output).to.equal(checkDateTimeRange(test.from, test.to));
            })
        });
    });


    mocha.checkLeaks();
    mocha.run();
</script>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console