user3286430
user3286430

Reputation:

Access a selected date value in jquery datepicker

I have a jquery date picker

$('#from').datepicker({
    dateFormat: "yy-mm-dd"
    });
    $('#to').datepicker({
    dateFormat: "yy-mm-dd"
    });

which i am trying to access the just selected date field for use in some other code.

This fetches the entire form field code console.log($("#from").get(0)); but i am only interested in the value that i can use in converting the dates to epoch

console.log($("#from").get(0));
    var from = $("#from").val().split('-');
    var to = $("#to").val().split('-');
    var epoch_from = new Date(from[0], from[1] - 1, from[2]).getTime() / 1000;
    var epoch_to = new Date(to[0], from[1] - 1, to[2]).getTime() / 1000;

    /**
    Epoch Stringfy
    */
    var from_string = epoch_from.toString();
    var to_string = epoch_to.toString();

How can i access the selected from and to values?.

Upvotes: 0

Views: 1104

Answers (2)

K K
K K

Reputation: 18099

You can use onSelect of datepicker and then you can use ui object which contains selectedDay, selectedMonth and selectedYear as separate values. That would be better for what you are trying to achieve:

JS

$(document).ready(function () {
    var dayObj;
    $('.checked').datepicker({
        onSelect: function (evt, ui) {
            dayObj = ui;
            console.log(ui.selectedDay, ui.selectedMonth, ui.selectedYear)
        }
    });

});

HTML:

<input type="text" class="checked" />
<div class="a"></div>

Demo: http://jsfiddle.net/lotusgodkk/GCu2D/358/

Upvotes: 0

mfarouk
mfarouk

Reputation: 654

you can use this function

var dateValue = $("#from").datepicker("getDate");

this function should return a date object

Upvotes: 1

Related Questions