{% dateDemoField = function( props ) { props = props || {} return '
' + ( props.pre || '' ) + '' + '
' } %} {% today = new Date(2013,3,20); today.setHours(0,0,0,0) %} {% nowDate = new Date(); now = nowDate.getHours() * 60 + nowDate.getMinutes(); now = now - now%30 %} {% playDate = new Date( today.getFullYear(), today.getMonth(), today.getDate() + 10 ) %}

The API§

You can play with the picker API to extend and create custom functionality:

var $input = $('.datepicker').pickadate()

// Use the picker object directly.
var picker = $input.pickadate('picker')
picker.methodName(argument1, argument2, ...)
picker.objectName

// Or pass through the element’s plugin data.
$input.pickadate(methodName, argument1, argument2, ...)
$input.pickadate(objectName)

For most examples on this page, the date picker is used, but the same API applies to the time picker as well.

Events§

There are six default events available in the options:

  1. onOpen
  2. onClose
  3. onStart
  4. onStop
  5. onRender
  6. onSet

Methods§

There are ten methods available on the picker:

  1. open
  2. close
  3. start
  4. stop
  5. render
  6. clear
  7. get
  8. set
  9. on
  10. trigger

All these methods (except get) return the picker object and are therefore chainable:

picker.open().clear().close()...

Objects§

There are four objects available through the picker:

  1. $node
  2. $root
  3. _hidden
  4. component

The Methods

Open and Close§

Open and close the picker holder. To check if it’s already open, use the get method.

picker.open()
picker.close()

// If a “click” is involved, prevent the event bubbling.
event.stopPropagation()
{%= dateDemoField({ id: 'demo__api-open-close', pre: '' }) %}

Close with focus§

Close the picker while keeping focus on the input element by passing a truth-y first argument.

picker.close(true)
{%= dateDemoField({ id: 'demo__api-close-focus', pre: '' }) %}

Open without focus§

Open the picker without focusing onto the input element by passing false as the first argument. Opening the picker this way, there’s a couple of things to note:

(1) The only way to close it is with a separate custom binding – in this example, on document click.

(2) The “opening” events are still triggered – however, using the get method to see if the picker is open will return false.

If any of the picker elements is focused/clicked, it resumes normal behavior.

picker.open(false)
$(document).on('click', function() {
    picker.close()
})
{%= dateDemoField({ id: 'demo__api-open-focus', pre: '' }) %}

Start and Stop§

Destroy and rebuild the picker.

picker.start()
picker.stop()
{%= dateDemoField({ id: 'demo__api-start-stop', pre: '' }).replace( 'type=text', 'type=date' ) %}

Render§

Refresh the picker after adding something to the holder.

picker.render()
{%= dateDemoField({ id: 'demo__api-render', pre: '' }) %}

Clear§

Clear the value in the picker’s input element.

picker.clear()
{%= dateDemoField({ id: 'demo__api-clear', pre: '', value: grunt.template.date('d mmmm, yyyy') }) %}

This is a shorthand that uses the set method behind the scenes.

Get§

Get the properties, objects, and states that make up the current state of the picker.

picker.get(thing)

The thing argument is an optional string and can be one of the following:

* Item Objects§

The things denoted in the list above with an asterisk (*) return a picker item object that can be formatted by passing a second string argument using the date or time formatting rules:

picker.get(thing, format)

Each “date” or “time” within the picker has an item object accompanying it behind the scenes.

Here’s a date picker item object for {%= grunt.template.date('d mmmm, yyyy') %}:

{
    // The full year.
    year: {%= today.getFullYear() %},

    // The month with zero-as-index.
    month: {%= today.getMonth() %},

    // The date of the month.
    date: {%= today.getDate() %},

    // The day of the week with zero-as-index.
    day: {%= today.getDay() %},

    // The underlying JavaScript Date object.
    obj: { '{%= today %}' },

    // The “pick” value used for comparisons.
    pick: {%= today.getTime() %}
}

Here’s a time picker item object for {%= grunt.template.date('h:MM TT') %}:

{
    // Hour of the day from 0 to 23.
    hour: {%= ~~(now/60) %},

    // The minutes of the hour from 0 to 59 (based on the interval).
    mins: {%= 30 + (now/60%1) * 60 %},

    // The “pick” value used for comparisons.
    pick: {%= 30 + now %}
}

Get value§

Returns the string value of the picker’s input element.

picker.get() // Short for `picker.get('value')`.
{%= dateDemoField({ id: 'demo__api-get--value', pre: '', value: grunt.template.date('d mmmm, yyyy'), placeholder: 'Open your console and try me…' }) %}

Get select§

Returns the item object that is visually selected.

picker.get('select')
{%= dateDemoField({ id: 'demo__api-get--select', pre: '', placeholder: 'Open your console and try me…' }) %}

Returns a formatted string for the item object that is visually selected.

picker.get('select', 'yyyy/mm/dd')
{%= dateDemoField({ id: 'demo__api-get--select-format', pre: '', placeholder: 'Open your console and try me…' }) %}

Get highlight§

Returns the item object that is visually highlighted.

picker.get('highlight')
{%= dateDemoField({ id: 'demo__api-get--highlight', pre: '', placeholder: 'Open your console and try me…' }) %}

Returns a formatted string for the item object that is visually highlighted.

picker.get('highlight', 'yyyy/mm/dd')
{%= dateDemoField({ id: 'demo__api-get--highlight-format', pre: '', placeholder: 'Open your console and try me…' }) %}

Get view§

Returns the item object that sets the current view.

picker.get('view')
{%= dateDemoField({ id: 'demo__api-get--view', pre: '', placeholder: 'Open your console and try me…' }) %}

Returns a formatted string for the item object that sets the current view.

picker.get('view', 'yyyy/mm/dd')
{%= dateDemoField({ id: 'demo__api-get--view-format', pre: '', placeholder: 'Open your console and try me…' }) %}

Get min§

Returns the item object that limits the picker’s lower range.

picker.get('min')
{%= dateDemoField({ id: 'demo__api-get--min', pre: '', placeholder: 'Open your console and try me…' }) %}

Returns a formatted string for the item object that limits the picker’s lower range.

picker.get('min', 'yyyy/mm/dd')
{%= dateDemoField({ id: 'demo__api-get--min-format', pre: '', placeholder: 'Open your console and try me…' }) %}

Get max§

Returns the item object that limits the picker’s upper range.

picker.get('max')
{%= dateDemoField({ id: 'demo__api-get--max', pre: '', placeholder: 'Open your console and try me…' }) %}

Returns a formatted string for the item object that limits the picker’s upper range.

picker.get('max', 'yyyy/mm/dd')
{%= dateDemoField({ id: 'demo__api-get--max-format', pre: '', placeholder: 'Open your console and try me…' }) %}

Get open§

Returns a boolean value of whether the picker is open or not.

picker.get('open')
{%= dateDemoField({ id: 'demo__api-get--open', pre: '', placeholder: 'Open your console and try me…' }) %}

Get start§

Returns a boolean value of whether the picker has started or not.

picker.get('start')
{%= dateDemoField({ id: 'demo__api-get--start', pre: '' + '', placeholder: 'Open your console and try me…' }).replace( 'type=text', 'type=date' ) %}

Get id§

Returns a unique 9-digit integer that is the ID of the picker.

picker.get('id')
{%= dateDemoField({ id: 'demo__api-get--id', pre: '', placeholder: 'Open your console and try me…' }) %}

Get disable§

Returns an array of items that determine which item objects to disable on the picker.

picker.get('disable')
{%= dateDemoField({ id: 'demo__api-get--disable', pre: '', placeholder: 'Open your console and try me…' }) %}

Set§

Set the properties, objects, and states to change the state of the picker.

picker.set(thing, value)

An alternate syntax can be used to set multiple things at once:

picker.set({
    thing: value,
    thing: value,
    thing: value
})

Both thing and value are required arguments. The value, is based on type of thing being set. The thing, is a string that can be:

* cascading changes§

When the things denoted in the list above with an asterisk (*) are set, they cascade into updating other things using the same value.

Set clear§

Clear the value in the picker’s input element.

picker.set('clear')
{%= dateDemoField({ id: 'demo__api-set-clear', pre: '', value: grunt.template.date('d mmmm, yyyy') }) %}

This is the full form of the clear method.

Set select§

Setting select has cascading changes that update the highlight, the view, and the value of the input element based on the settings format.

Select a date item object§

// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('select', [{%= today.getFullYear() + ',' + today.getMonth() + ',' + today.getDate() %}])

// Using JavaScript Date objects.
picker.set('select', { '{%= playDate %}' })

// Using positive integers as UNIX timestamps.
picker.set('select', {%= today.getTime() - 468487654 %})
{%= dateDemoField({ id: 'demo__api-set--select-date', pre: '' + '' + '' }) %}

Select a time item object§

// Using arrays formatted as [HOUR,MINUTE].
picker.set('select', [3,0])

// Using positive integers as minutes.
picker.set('select', 540)
{%= dateDemoField({ id: 'demo__api-set--select-time', pre: '' + '' }) %}

Set highlight§

Setting highlight has a cascading change that updates the item object that sets the view of the picker.

Highlight a date item object§

// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('highlight', [{%= today.getFullYear() + ',' + today.getMonth() + ',' + today.getDate() %}])

// Using JavaScript Date objects.
picker.set('highlight', { '{%= playDate %}' })

// Using positive integers as UNIX timestamps.
picker.set('highlight', {%= today.getTime() - 468487654 %})
{%= dateDemoField({ id: 'demo__api-set--highlight-date', pre: '' + '' + '' }) %}

Highlight a time item object§

// Using arrays formatted as [HOUR,MINUTE].
picker.set('highlight', [15,30])

// Using positive integers as minutes.
picker.set('highlight', 1080)
{%= dateDemoField({ id: 'demo__api-set--highlight-time', pre: '' + '' }) %}

Set view§

Setting view has no cascading changes and the highlight remains unaffected.

Viewset a date item object§

The value passed gets normalized to the first date of the month to bring into view.

// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('view', [2000,3,20])

// Using JavaScript Date objects.
picker.set('view', { '{%= new Date(1988,7,14) %}' })

// Using positive integers as UNIX timestamps.
picker.set('view', 1587355200000)
{%= dateDemoField({ id: 'demo__api-set--view-date', pre: '' + '' + '' }) %}

Viewset a time item object§

// Using arrays formatted as [HOUR,MINUTE].
picker.set('view', [15,30])

// Using positive integers as minutes.
picker.set('view', 1080)
{%= dateDemoField({ id: 'demo__api-set--view-time', pre: '' + '' }) %}

Set min§

Setting min has cascading changes on the select, highlight, and view only when the particular item object goes out of range.

Scope the date item objects§

// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('min', [2013,3,20])

// Using JavaScript Date objects.
picker.set('min', { '{%= new Date(2013,7,14) %}' })

// Using integers as days relative to today.
picker.set('min', -4)

// Using `true` for “today”.
picker.set('min', true)

// Using `false` to remove.
picker.set('min', false)
{%= dateDemoField({ id: 'demo__api-set--min-date', pre: '' + '' + '' + '' + '' }) %}

Scope the time item objects§

// Using arrays formatted as [HOUR,MINUTE].
picker.set('min', [15,30])

// Using integers as intervals relative from now.
picker.set('min', -4)

// Using `true` for “now”.
picker.set('min', true)

// Using `false` to remove.
picker.set('min', false)
{%= dateDemoField({ id: 'demo__api-set--min-time', pre: '' + '' + '' + '' }) %}

Set max§

Setting max has cascading changes on the select, highlight, and view only when the particular item object goes out of range.

Scope the date item objects§

// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('max', [2013,3,20])

// Using JavaScript Date objects.
picker.set('max', { '{%= new Date(2013,7,14) %}' })

// Using integers as days relative to today.
picker.set('max', 4)

// Using `true` for “today”.
picker.set('max', true)

// Using `false` to remove.
picker.set('max', false)
{%= dateDemoField({ id: 'demo__api-set--max-date', pre: '' + '' + '' + '' + '' }) %}

Scope the time item objects§

// Using arrays formatted as [HOUR,MINUTE].
picker.set('max', [15,30])

// Using integers as intervals relative from now.
picker.set('max', 4)

// Using `true` for “now”.
picker.set('max', true)

// Using `false` to remove.
picker.set('max', false)
{%= dateDemoField({ id: 'demo__api-set--max-time', pre: '' + '' + '' + '' }) %}

The Events and Methods

Event on§

Bind callbacks to get fired off when the relative picker method is called:

picker.on(methodName, function() { … })

An alternate syntax can be used to bind multiple callbacks at once:

picker.on({
    methodName: function() { … },
    methodName: function() { … },
    methodName: function() { … }
})

The methodName can be open, close, render, start, stop, or set.

picker.on('open', function() {
    console.log( 'Opened.. and here I am!' )
})
{%= dateDemoField({ id: 'demo__api-method-on', placeholder: 'Open your console and try me…' }) %}

Default Events§

The on method can only be used after the picker has been initiated. To set default events, pass them as options when invoking the picker:

$('.datepicker').pickadate({
    onOpen: function() {
        console.log('Opened up!')
    },
    onClose: function() {
        console.log('Closed now')
    },
    onRender: function() {
        console.log('Just rendered anew')
    },
    onStart: function() {
        console.log('Hello there :)')
    },
    onStop: function() {
        console.log('See ya')
    },
    onSet: function(event) {
        console.log('Set stuff:', event)
    }
})
{%= dateDemoField({ id: 'demo__api-default-events', placeholder: 'Open your console and try me…', pre: '' }).replace( 'type=text', 'type=date' ) %}

Scope and Arguments§

Within scope of the events’ callbacks, this refers to the picker object – and for most events, no arguments are passed.

The only exception is the set method, which is passed an argument that provides more context as to what is being “set”.

Event trigger§

Trigger callbacks that have been queued up using the the on method:

picker.on('open', function() {
    console.log('Didn’t open.. yet here I am!')
})
picker.trigger('open')
{%= dateDemoField({ id: 'demo__api-method-trigger', placeholder: 'Open your console and try me…', pre: '' }) %}

The Objects

Object $node§

This is the picker’s relative input element wrapped as a jQuery object.

picker.$node
{%= dateDemoField({ id: 'demo__api-object--node', pre: '', placeholder: 'Open your console and try me…' }) %}

Object $root§

This is the picker’s relative root holder element wrapped as a jQuery object.

picker.$root
{%= dateDemoField({ id: 'demo__api-object--holder', pre: '', placeholder: 'Open your console and try me…' }) %}

Object _hidden§

This is the picker’s relative hidden element, which is undefined if there’s no formatSubmit option.

There should be no reason to use this – it’s mostly for internal use. If you have a valid reason for using this, please mention it in the Issues.

Object component§

This is the picker’s relative component constructor that builds the date or time picker. This API is in flux – so avoid using it for now.