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.
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()
Close the picker while keeping focus on the input
element by passing a truth-y first argument.
picker.close(true)
Open the picker without focusing onto the input
element by passing false
as the first argument. Opening the picker this way, there’s a few 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
.
(3) If any of the picker elements is focused/clicked, it resumes normal behavior.
picker.open(false)
$(document).on('click', function() {
picker.close()
})
Destroy and rebuild the picker. To check if it’s already started, use the get
method.
picker.stop()
picker.start()
Destroying the picker also clears any events’ callbacks bound on it.
Refresh the picker after adding something to the holder.
picker.render()
By default, only the “face” of the picker (i.e. the box
element), has it’s contents re-rendered. To render the entire picker from the root
up, pass true
as the first argument:
picker.render(true)
Clear the value in the picker’s input
element.
picker.clear()
This is a shorthand that uses the set
method behind the scenes.
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:
The thing
s 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 20 April, 2013:
{
// The full year.
year: 2013,
// The month with zero-as-index.
month: 3,
// The date of the month.
date: 20,
// The day of the week with zero-as-index.
day: 6,
// The underlying JavaScript Date object.
obj: { 'Sat Apr 20 2013 00:00:00 GMT-0400 (EDT)' },
// The “pick” value used for comparisons.
pick: 1366430400000
}
Here’s a time picker item object for 4:20 PM:
{
// Hour of the day from 0 to 23.
hour: 16,
// The minutes of the hour from 0 to 59 (based on the interval).
mins: 20,
// The “pick” value used for comparisons.
pick: 980
}
value
§Returns the string value of the picker’s input
element.
picker.get() // Short for `picker.get('value')`.
select
§Returns the item object that is visually selected.
picker.get('select')
Returns a formatted string for the item object that is visually selected.
picker.get('select', 'yyyy/mm/dd')
highlight
§Returns the item object that is visually highlighted.
picker.get('highlight')
Returns a formatted string for the item object that is visually highlighted.
picker.get('highlight', 'yyyy/mm/dd')
view
§Returns the item object that sets the current view.
picker.get('view')
Returns a formatted string for the item object that sets the current view.
picker.get('view', 'yyyy/mm/dd')
min
§Returns the item object that limits the picker’s lower range.
picker.get('min')
Returns a formatted string for the item object that limits the picker’s lower range.
picker.get('min', 'yyyy/mm/dd')
max
§Returns the item object that limits the picker’s upper range.
picker.get('max')
Returns a formatted string for the item object that limits the picker’s upper range.
picker.get('max', 'yyyy/mm/dd')
disable
and enable
§Both these things work together to determine which item objects to disable on the picker:
// Array passed in options to disable dates
var datesToDisable = [ 1, 4, 7, [2013,3,8], [2013,3,19], new Date(2013,3,26) ]
// Returns `1` to represent a base “enabled” state
picker.get('enable')
// Returns the collection of dates to disable
picker.get('disable')
However, when all dates are disabled and a select few are enabled, it behaves differently:
// Array passed in options to disable all dates except a few
var datesToDisable = [ true, 1, 4, 7, [2013,3,8], [2013,3,19], new Date(2013,3,26) ]
// Returns `-1` to represent a base “flipped” state
picker.get('enable')
// Returns the collection of dates to *not* disable
picker.get('disable')
Set the properties, objects, and states to change the picker.
// One at a time
picker.set(thing, value)
// Multiple at once
picker.set({
thing: value,
thing: value,
thing: value
})
The value
is based on the thing
being set. The thing
, is a string that can be:
When the thing
s denoted in the list above with an asterisk (*) are set, they cascade into updating other things using the same value.
By default, any callbacks bound with the on
method will be fired. To silenty set a thing
, pass an options object with the muted
parameter set to true
:
// One at a time
picker.set(thing, value, { muted: true })
// Multiple at once
picker.set({
thing: value,
thing: value,
thing: value
}, { muted: true })
clear
§Clear the value in the picker’s input
element.
picker.set('clear')
This is the full form of the clear
method.
select
§Setting select
has cascading changes that update the highlight
, the view
, and the value of the input
element based on the settings format
.
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('select', [2013,3,20])
// Using JavaScript Date objects.
picker.set('select', { 'Tue Apr 30 2013 00:00:00 GMT-0400 (EDT)' })
// Using positive integers as UNIX timestamps.
picker.set('select', 1366898887654)
// Using arrays formatted as [HOUR,MINUTE].
picker.set('select', [3,0])
// Using JavaScript Date objects.
picker.set('select', { 'Thu Apr 25 2013 10:08:07 GMT-0400 (EDT)' })
// Using positive integers as minutes.
picker.set('select', 540)
highlight
§Setting highlight
has a cascading change that updates the item object that sets the view
of the picker.
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('highlight', [2013,3,20])
// Using JavaScript Date objects.
picker.set('highlight', { 'Tue Apr 30 2013 00:00:00 GMT-0400 (EDT)' })
// Using positive integers as UNIX timestamps.
picker.set('highlight', 1366898887654)
// Using arrays formatted as [HOUR,MINUTE].
picker.set('highlight', [15,30])
// Using JavaScript Date objects.
picker.set('highlight', { 'Thu Apr 25 2013 10:08:07 GMT-0400 (EDT)' })
// Using positive integers as minutes.
picker.set('highlight', 1080)
view
§Setting view
has no cascading changes and the highlight
remains unaffected.
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', { 'Sun Aug 14 1988 00:00:00 GMT-0400 (EDT)' })
// Using positive integers as UNIX timestamps.
picker.set('view', 1587355200000)
// Using arrays formatted as [HOUR,MINUTE].
picker.set('view', [15,30])
// Using JavaScript Date objects.
picker.set('view', { 'Thu Apr 25 2013 10:08:07 GMT-0400 (EDT)' })
// Using positive integers as minutes.
picker.set('view', 1080)
min
§Setting min
has cascading changes on the select
, highlight
, and view
only when the particular item object goes out of range.
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('min', [2013,3,20])
// Using JavaScript Date objects.
picker.set('min', { 'Wed Aug 14 2013 00:00:00 GMT-0400 (EDT)' })
// 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)
// Using arrays formatted as [HOUR,MINUTE].
picker.set('min', [15,30])
// Using JavaScript Date objects.
picker.set('min', { 'Thu Apr 25 2013 10:08:07 GMT-0400 (EDT)' })
// 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)
max
§Setting max
has cascading changes on the select
, highlight
, and view
only when the particular item object goes out of range.
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('max', [2013,3,20])
// Using JavaScript Date objects.
picker.set('max', { 'Wed Aug 14 2013 00:00:00 GMT-0400 (EDT)' })
// 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)
// Using arrays formatted as [HOUR,MINUTE].
picker.set('max', [15,30])
// Using JavaScript Date objects.
picker.set('max', { 'Thu Apr 25 2013 10:08:07 GMT-0400 (EDT)' })
// 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)
disable
and enable
§Setting disable
or enable
has cascading changes on the select
, highlight
, and view
only when the currently selected item object becomes disabled.
An important thing to note here is that “setting” something as enabled or disabled adds the new elements to the collection of items to disable rather than completely replacing them with the new collection.
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('disable', [ [2013,9,3], [2013,9,9], [2013,9,20] ])
// Using JavaScript Date objects.
picker.set('disable', [ new Date(2013,9,13), new Date(2013,9,24) ])
// Using integers representing the days of the week (from 1 to 7).
picker.set('disable', [ 1, 4, 7 ])
// Using `true` to disable all the dates.
picker.set('disable', true)
// Using `false` to clear the collection.
picker.set('disable', false)
// Or “flip” to switch the enabled and disabled dates.
picker.set('disable', 'flip')
// Using arrays formatted as [YEAR,MONTH,DATE].
picker.set('enable', [ [2013,9,3], [2013,9,9], [2013,9,20] ])
// Using JavaScript Date objects.
picker.set('enable', [ new Date(2013,9,13), new Date(2013,9,24) ])
// Using integers representing the days of the week (from 1 to 7).
picker.set('enable', [ 1, 4, 7 ])
// Using `true` to enable all the dates.
picker.set('enable', true)
// Using `false` to clear the collection.
picker.set('enable', false)
// Or “flip” to switch the enabled and disabled dates.
picker.set('enable', 'flip')
// Using arrays formatted as [HOUR,MINUTES].
picker.set('disable', [ [2,30], [4,30], [9,0] ])
// Using JavaScript Date objects.
picker.set('disable', [ new Date(2013,9,13,6), new Date(2013,9,13,12,30) ])
// Using integers representing the days of the week (from 1 to 7).
picker.set('disable', [ 1, 4, 7 ])
// Using `true` to disable all the times.
picker.set('disable', true)
// Using `false` to clear the collection.
picker.set('disable', false)
// Or “flip” to switch the enabled and disabled times.
picker.set('disable', 'flip')
// Using arrays formatted as [HOUR,MINUTES].
picker.set('enable', [ [2,30], [4,30], [9,0] ])
// Using JavaScript Date objects.
picker.set('enable', [ new Date(2013,9,13,6), new Date(2013,9,13,12,30) ])
// Using integers representing the days of the week (from 1 to 7).
picker.set('enable', [ 1, 4, 7 ])
// Using `true` to enable all the times.
picker.set('enable', true)
// Using `false` to clear the collection.
picker.set('enable', false)
// Or “flip” to switch the enabled and disabled times.
picker.set('enable', 'flip')
When a date or time you want to enable/disable falls within a range of the opposite state, pass 'inverted'
as the last item in the array:
// Disable all Mondays, except November 17th, 2013.
picker.set('disable', [ 1, [2013, 10, 17, 'inverted'] ])
// Disable all times from 1:00 AM to 1:59 AM, except 1:30 AM.
picker.set('disable', [ 1, [1, 30, 'inverted'] ])
interval
§For the time picker only, you can change the interval between each time display.
Setting interval
has cascading changes on the select
, highlight
, and view
only when the particular item object goes out of range.
// Using integers representing the interval length in minutes
picker.set('interval', 15)
picker.set('interval', 20)
picker.set('interval', 120)
on
§Bind callbacks to get fired off when the relative picker method is called (unless if the callback is “muted”):
// One at a time
picker.on(methodName, function() { … })
// Multiple at once
picker.on({
methodName: function() { … },
methodName: function() { … },
methodName: function() { … }
})
The methodName
can be open
, close
, render
, start
, stop
, or set
.
Within scope of these 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”.
After the picker has been initiated, callbacks to events can be set using the on
method:
picker.on({
open: function() {
console.log('Opened up!')
},
close: function() {
console.log('Closed now')
},
render: function() {
console.log('Just rendered anew')
},
stop: function() {
console.log('See ya')
},
set: function(thingSet) {
console.log('Set stuff:', thingSet)
}
})
Since these callbacks can only be bound after the picker has started, the start
event cannot be given a callback this way.
Before the picker has initiated, callbacks to events can be set by passing 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(thingSet) {
console.log('Set stuff:', thingSet)
}
})
trigger
§Trigger an event’s callbacks that have been queued up:
picker.on('open', function() {
console.log('Didn’t open.. yet here I am!')
})
picker.trigger('open')
$root
§This is the picker’s relative root holder element wrapped as a jQuery object.
picker.$root
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.