Mevia
Mevia

Reputation: 1564

jQuery date & time picker

Since i am working a lot with datetime in my project and it mostly is either feched or inserted into db, i found myselfe in need of some jQuery widget that could help dealing with this situation.

I am using MySQL datetime field for it which is formatted in following way:

yyyy-mm-dd hh:mm:ss

I tried to avoid some big plugins and create something on my own. While writing it i have encountered 2 problems.

JS:

$(function() {

    $("#datepicker").datepicker();
    $("#datepicker").change(function() {
        $("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd");
    });

    $( "#hours" ).slider({value:0, min: 0, max: 23, step: 1,
        slide: function( event, ui ) {
            $( "#pickhour" ).val( ui.value );
        }
    });

    $( "#minutes" ).slider({value:0, min: 0, max: 59, step: 1,
        slide: function( event, ui ) {
            $( "#pickminute" ).val( ui.value );
        }
    });

    $( "#seconds" ).slider({value:0, min: 0, max: 59, step: 1,
        slide: function( event, ui ) {
            $( "#picksecond" ).val( ui.value );
        }
    });
    $( "#pickhour" ).val( $( "#hours" ).slider( "value" ) );
    $( "#pickminute" ).val( $( "#minutes" ).slider( "value" ) );
    $( "#picksecond" ).val( $( "#seconds" ).slider( "value" ) );

});

HTML:

    <p>Data: <input type="text" name="datepicker" id="datepicker" size="8"></p>
    <p>Czas: <input type="text" name="pickhour" id="pickhour" size="1" readonly>
             <input type="text" name="pickminute" id="pickminute" size="1" readonly>
             <input type="text" name="picksecond" id="picksecond" size="1" readonly>
    </p>
    <p><div id="hours" style="width:10%;"></div></p>
    <p><div id="minutes" style="width:10%;"></div></p>
    <p><div id="seconds" style="width:10%;"></div></p>

Fiddle Link:

http://jsfiddle.net/hQ4rZ/

So the first problem regards time processing:

Other problem regards displaying entire datetime when its ready:

is there someone who could help with that ?;)

Upvotes: 0

Views: 593

Answers (1)

Sujit Patil
Sujit Patil

Reputation: 183

Data:

Czas:

$(function() {

$("#datepicker").datepicker();
$("#datepicker").change(function() {
    $("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd");
});

$( "#hours" ).slider({value:0, min: 0, max: 23, step: 1,
    slide: function( event, ui ) {
        console.log(ui.value.toString().length);
        $( "#pickhour" ).val( ui.value.toString().length==1? "0"+ui.value :ui.value);
    }
});

$( "#minutes" ).slider({value:0, min: 0, max: 59, step: 1,
    slide: function( event, ui ) {
        $( "#pickminute" ).val(  ui.value.toString().length==1? "0"+ui.value :ui.value);
    }
});

$( "#seconds" ).slider({value:0, min: 0, max: 59, step: 1,
    slide: function( event, ui ) {
        $( "#picksecond" ).val(  ui.value.toString().length==1? "0"+ui.value :ui.value );
    }
});
$( "#pickhour" ).val( $( "#hours" ).slider( "value" ) );
$( "#pickminute" ).val( $( "#minutes" ).slider( "value" ) );
$( "#picksecond" ).val( $( "#seconds" ).slider( "value" ) );
$("#Done").on("click",function(){
    var dateVal=  $("#datepicker").val() +" " +  $("#pickhour").val()+":" + $("#pickminute").val()+":" + $("#picksecond").val() ;
    alert(dateVal);
})

});

Upvotes: 1

Related Questions