0

I'm not sure why the following is happening but, I'm using datetimepicker1 from bootstrap, and when I try to get the selected value, I get undefined for

var date =  $("#datetimepicker1").find("input").val();

but get the right date when I type $("#datetimepicker1").find("input").val(); on the console. HOw can I get the value of the date picked/selected so I can use it?

html

<div class="container">
<div class="row">
    <div class='col-sm-6'>
        <div class="form-group">
            <div class='input-group date' id='datetimepicker1'>
                <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
</div>

Also, Im trying to change the format of my date to "Jan 14 2016 hh:min:ss" using "M dd yyyy hh:ii:ss" but it gives me Fri (weekdays) instead of month names... The link I'm using is this one enter link description here

user65165
  • 874
  • 1
  • 8
  • 11

2 Answers2

1

Try without the find.

 var date = $("#datetimepicker1").val();
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8
1
<!--- in Html code, add id in input -->
<input type='text' id="date_time" class="form-control" />

// In Jquery
var date = $("#date_time").val();
SuperNova
  • 25,512
  • 7
  • 93
  • 64
  • But to get the value of the input, you have to refer to the input. If you refer to ID of the div, you get the value of the div. Also in the demo they wont give ID for input. – SuperNova Jan 28 '16 at 15:30
  • selecting the date from calendar fills up the input field. The value you are looking for is inside the input. So add a ID to the input and get the value from the same. – SuperNova Jan 28 '16 at 15:35
  • also, the problem with this is that it doesn't dynamically change whenever I check another inpt in the calendar. I'd have to keep calling var date = $("#date_time").val(); – user65165 Jan 28 '16 at 15:43
  • format is something like this 2016-01-28 21:20:20..... So just slice the date in javascript as date.slice(0,19) – SuperNova Jan 28 '16 at 15:48
  • No. Its got to change. You mean to say you have another(in addition to this) input as datetime ? If you change the same input and check, the value will change for this input... – SuperNova Jan 28 '16 at 15:49
  • no, when i select a date from the calendar i only get the value in the console after I retype var date bla bla bla. date doesn't update. also, date as a variable returns undefined when declared out of a function. – user65165 Jan 28 '16 at 15:56
  • No. The input will change when you change and select another datetime – SuperNova Jan 28 '16 at 15:56
  • can you provide a link to the working example? in jsfiddle or plunker? Also if you want to access the date outside of function, you will have to define it outside of the function. And then make a change to value inside function. the date(global) gets changed.. – SuperNova Jan 28 '16 at 15:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101913/discussion-between-user65165-and-trying2learn). – user65165 Jan 28 '16 at 16:00