Skip to main content Skip to docs navigation

Date Functions

On this page
For the sake of the following documentation, assume there's a picker setup like this:
const picker = new tempusdominus
  .TempusDominus(document.getElementById('datetimepicker1'));

Getting the View Date

picker.viewDate returns the pickers current view date.

picker.dates

There are a number of function here that allow for retrieving the selected dates or adding to them. These functions are used as picker.dates.add(...) for example.

picked

Returns an array of DateTime of the selected date(s).

lastPicked

Returns the last picked DateTime of the selected date(s).

lastPickedIndex

Returns the length of picked dates -1 or 0 if none are selected.

add(DateTime)

Adds a new DateTime to selected dates array. Use this function with caution. It will not automatically refresh the widget or do any validation.

setValue(value: DateTime, index?: number)

Sets the select date index (or the first, if not provided) to the provided DateTime object.

formatInput(value: DateTime): string

Formats a DateTime object to a string. Used when setting the input value. It is possible to overwrite this to provide more complex formatting with moment/dayjs or by hand.

parseInput(value:any): DateTime

Parse the value into a DateTime object. This can be overwritten to supply your own parsing.

setFromInput(value: any, index?: number)

Tries to convert the provided value to a DateTime object. If value is null|undefined then clear the value of the provided index (or 0). It is possible to overwrite this to provide more complex formatting with moment/dayjs or by hand.

isPicked(DateTime, Unit?)

Returns true if the target date is part of the selected dates array. If unit is provided then a granularity to that unit will be used.

pickedIndex(DateTime, Unit?)

Returns the index at which target date is in the array. This is used for updating or removing a date when multi-date is used. If unit is provided then a granularity to that unit will be used.

clear

Clears all selected dates.

Emits Namespace.events.change with the last picked date.