4

I'm implementing a pickadate date picker, and it's not clear in the docs how to actually use the date a user selects. Eg I have this written:

$('.datepicker').pickadate({
    onSet: function(context) {
        console.log(context);
    }
});

This produces a weird output (for Sep 8 2015):

Object {select: 1441666800000}

How is this output actually usable?

j_d
  • 2,818
  • 9
  • 50
  • 91

4 Answers4

9

Your output is a javascript object, with a property select. Value of this property is a timestamp. Actually this timestamp is amount of milliseconds from the start of unix epoch to the selected day. You can transform in into a date object by using Date.prototype:

$('.datepicker').pickadate({
    onSet: function(context) {
        console.log(new Date(context.select));
    }
});
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
  • 1
    Cool. Any idea how to get pickadate to not print the chosen date to the input by default? – j_d Sep 23 '15 at 09:49
  • How would you go about instead of showing this selected date, you had to capture it in a variable to later use somewhere else? Where does this code goes? I'm sorry for asking on an old answer but I have found only 2 pieces of code that resemble what I need... – keont Jul 13 '16 at 09:19
  • 1
    https://gist.github.com/diedsmiling/788d3684b0e8eec804b31cdd7bc9c8e3 something like this, if I understood you correctly. – Alexandr Lazarev Jul 13 '16 at 09:22
  • Exactly right, thank you a lot. It might seem easy but for me that I haven't done this, I was looking in every single wrong place it seems. – keont Jul 13 '16 at 10:23
6

The weird number is the date returned in milliseconds.

Try this in a console:

new Date(1441666800000);

// Returns:
// Mon Sep 07 2015 16:00:00 GMT-0700 (PDT)

Used like this:

$('.datepicker').pickadate({
    onSet: function(context) {
        console.log(new Date(context.select););
    }
});

Hope this helps!

Armando Canals
  • 290
  • 1
  • 7
6

You can also make use of the get select method to get the selected date in a more convenient format. For example when the close event is triggered.

$('.datepicker').pickadate({
    onClose: function() {
        console.log(this.get('select', 'yyyy-mm-dd'));
    }
});
stpe
  • 3,611
  • 3
  • 31
  • 38
0

First you have to add datepicker for input field and then on submit button you can store the selcected value to a variable and use variable where you want to show date using jquery:

<script type="text/javascript">
// Datepicker
$(document).ready(function(){
    $("#datepicker" ).datepicker({ minDate: 0});    
    jQuery( "#datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });

    $("form").submit(function() {
        var datepickedup = $("input#datepicker").val();
    });
});

Where #datepicker is input field id.

Nisha Sharma
  • 245
  • 1
  • 2
  • 13