Regular Expression to validate a timestamp

后端 未结 13 550
名媛妹妹
名媛妹妹 2021-01-01 01:19

I need a regular expression to validate a timestamp of the format, using Javascript:

YYYY/MM/DD HH:MI:SS

I trie

相关标签:
13条回答
  • 2021-01-01 02:08
    function validateTimestamp(timestamp) {
    
        if (!/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}/.test(timestamp)) {
            return false;
        }
    
        var split = timestamp.split(/[^\d]+/);
    
        var year = parseFloat(split[0]);
        var month = parseFloat(split[1]);
        var day = parseFloat(split[2]);
    
        var hour = parseFloat(split[3]);
        var minute = parseFloat(split[4]);
        var second = parseFloat(split[5]);
    
        return hour < 25 && minute < 61 && second < 61 && month < 13 && day < 32;
    
    }
    
    0 讨论(0)
提交回复
热议问题