Get the value of bootstrap Datetimepicker in JavaScript

前端 未结 8 1984
心在旅途
心在旅途 2020-11-30 05:49

I need to get the value of Datetimepicker in my JavaScript function. I have made something like this, but it doesn\'t work:

$(\"#date\").click( function(){
         


        
8条回答
  •  情深已故
    2020-11-30 06:19

    Either use:

    $("#datetimepicker1").data("datetimepicker").getDate();
    

    Or (from looking at the page source):

    $("#datetimepicker1").find("input").val();
    

    The returned value will be a Date (for the first example above), so you need to format it yourself:

    var date = $("#datetimepicker1").data("datetimepicker").getDate(),
        formatted = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours + ":" + date.getMinutes() + ":" + date.getSeconds();
    alert(formatted);
    

    Also, you could just set the format as an attribute:

    and you could use the $("#datetimepicker1").find("input").val();

提交回复
热议问题