Cannot convert a local variable into a global one in jquery [duplicate]

I’m trying to set up a date range picker for a project I’m working on with some others. I’ve been able to get the input of the user and store it in a variable, but only locally within its function. Nothing I try can get the variable into a global scope where it can be used more effectively.

Here’s the code I’ve been using. It was to my understanding that declaring the variables globally and assigning their values within the function without the var keyword would set the variable as global rather than local, which it does do in regular javascript, but not in any of my jquery. Any input on what I’m doing wrong would be appreciated.

$(function() {
    var startTime
    var endTime
    $('input[name="datetimes"]').daterangepicker({
        timePicker: true,
        startDate: moment().startOf('hour'),
        endDate: moment().startOf('hour').add(32, 'hour'),
        locale: {
        format: 'hh:mm A'
        }
    });
    $('#daterange').on('apply.daterangepicker', function(ev, picker) {
        startTime = (picker.startDate.format('YYYY-MM-DD h:mm A'));
        endTime = (picker.endDate.format('YYYY-MM-DD h:mm A'));
        console.log(startTime)
        console.log(endTime)
      });
    console.log(startTime)
    console.log(endTime)
});