1

I have an HTML page, and I have 2 calenders in it. A "Start Date" and an "End Date"

 <label for="startdatepicker" class="col-sm-2 control-label">Start Date</label>
    <div class="container">
        <div class="row">
            <div class='col-sm-6'>
                <div class="form-group">
                    <div class='input-group date' value="2018-01-01" name="StartDate" id='startdatepicker'>
                        <input type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calender"></span>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>


<label for="enddatepicker" class="col-sm-2 control-label">End Date</label>
    <div class="container">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <div class="input-group date" id="enddatepicker">
                        <input type="text" class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calender"></span>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

Here is a JavaScript that has converted some other data successfully into JSON. (From a drop down selection list)

What I'm trying to do is on the 'Click' of a button, I am converting the following data into JSON. Everything besides the Calendar select is working.

Instead of outputting the dates, it outputs an empty "".

 $("#requestdata").click(function () {

    var sourceserver = $("#sourceserver").val();
    var orgnumber = $("#orgnumber").val();
    var startdate = $("#startdatepicker").val();
    var enddate = $("#enddatepicker").val();


    var jsonrequest = {
        SourceServer: sourceserver,
        OrgNumber: orgnumber,
        StartDate: startdate,
        EndDate: enddate
    };

    var jsonString = JSON.stringify(jsonrequest);
    console.log(jsonString);

});

My current output is:

{"SourceServer":"CHS-HISTORIAN","OrgNumber":"5679 - Stn001ChsWinLagBlwr","StartDate":"","EndDate":""}

The 'button' HTML if it matters

<input type="button" class="btn btn-primary col-sm-offset-2" id="requestdata" value="Request Data" /> 

Basically I'm having lots of trouble converting the selected Calendar Dates into a JSON.

Thanks!

EDIT 1:

In the JavaScript code, I changed var startdate = $("#startdatepicker").val();

to

 var startdate = $("#startdatepicker").data("DateTimePicker");

And now the console is returning an empty object

{}

instead of just quotations ""

The object is still empty for some reason though..

  • `#startdatepicker` and `#enddatepicker` are `
    `s. They have no "value" you could read with `.val()`. Check the documentation of the used date picker library on how to get the selected date.
    – Andreas Mar 06 '18 at 16:47
  • @Andreas I just followed what the library said to do and they said to use s, which was https://eonasdan.github.io/bootstrap-datetimepicker/ – zzzzzzzzzzz Mar 06 '18 at 16:50
  • Have a look at the _big blue box at the top of the page_ and the _"Functions"_ section – Andreas Mar 06 '18 at 16:52
  • Possible duplicate of [Can't get date object from Bootstrap DateTimePicker via JQuery](https://stackoverflow.com/questions/38148966/cant-get-date-object-from-bootstrap-datetimepicker-via-jquery) – Andreas Mar 06 '18 at 16:54
  • Hi thank you for the link, but that thread didn't work, getting the same result – zzzzzzzzzzz Mar 06 '18 at 17:07

0 Answers0