Is there a plugin that serves as a mask and validator for 24h hours?

2

I searched but found only plugins for time with "select" and the times in the AM / PM formed.

I would like one to be able to type and stay in this format:

23:59:59
    
asked by anonymous 08.06.2015 / 21:36

3 answers

3

This is more or less simple to do, ie a mask to insert : when you are inserting numbers.

It may be more complex to allow too many large numbers and to exclude letters.

A simple example would be:

document.getElementById('hora').addEventListener('keyup', function(e){
    var str = this.value.split(':').join('');
    if (str.length > 6) str = str.slice(0, 6);
    var formatado = str.match(/.{1,2}/g).join(':');
    this.value = formatado;
});

jsFiddle: link

These 4 lines of code explained:

  • remove : from any string
  • remove the last character if it is too long
  • Break into two-character peds and paste back with : between them
  • put it back in the input
08.06.2015 / 21:46
1

See if this helps:

var time = new Date(params.tweetDate),
h = time.getHours(), // 0-24 format
m = time.getMinutes();

Font: link

    
08.06.2015 / 21:44
0

I used the jQuery-Mask-Plugin plugin: link

And I did the 24 hour time validations that I needed. It does not allow 24:59:59, 23:69:59 or 23:59:69, for example.

<script type="text/javascript" src="~/Scripts/jQueryMask/jquery.mask.js">

</script>

<script type="text/javascript">
    $(document).ready(function () {
        var options = {
            onKeyPress: function (a, b, c) {
                if (a.length == 2) {
                    if (parseInt(a[1]) > 3 && parseInt(a[0]) == 2) {
                        a = a[0];
                        $(c).val(a);
                    }
                }
            },
            translation: {
                A: { pattern: /[0-2]/ },
                C: { pattern: /[0-5]/ },
                B: { pattern: /[0-9]/ }
            }
        };

        $('.time').mask('AB:CB:CB', options);
    })
</script>
    
08.06.2015 / 23:27