小编Ben*_*ava的帖子

将数字格式化为时间,如hh:mm

我有一个输入字段:

<input type="text" name="time" class="time" value="3" />
Run Code Online (Sandbox Code Playgroud)

我需要这个价值就像03:00

更多我需要的例子:

1 = 01:00
12 = 12:00
12:2 = 12:20
2:2 = 02:20
02:2 = 02:20
340 = 340:00
340:1 = 340:10
Run Code Online (Sandbox Code Playgroud)

你知道其余的.我怎样才能在jquery/javascript中解决这个问题?

这是我在jQuery中尝试的:

$('input').blur(timeFormat);

function timeFormat(e){
    skjema.find('input.tid').each(function(){
        if($(this).val().length != 0){
            var tid = $(this).val().toString();

            if(tid.length == 1){

                $(this).val(String("0" + tid));

            }
            if(tid.indexOf(':') == -1){
                $(this).val(tid.toString() + ':00');
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这就是我现在所做的,它完成了这项工作,但它有点笨重:)

function timeFormat(e){
    skjema.find('input.tid').each(function(){
        if($(this).val().length != 0){
            var tid = $(this).val().toString();
            tid = (tid.length == 1) ? '0' + tid …
Run Code Online (Sandbox Code Playgroud)

javascript format formatting jquery

4
推荐指数
1
解决办法
8874
查看次数

标签 统计

format ×1

formatting ×1

javascript ×1

jquery ×1