@nativescript/datetimepicker

A plugin that provides the UI elements DatePickerField, TimePickerField and DateTimePickerFields for picking a date and a time. The DatePickerField and TimePickerField components extend the TextField view, while the DateTimePickerFields extends the GridLayout layout container that contains instances of the DatePickerField and TimePickerField components. There is also a DateTimePicker class which provides static methods pickDate() and pickTime() that can be called to show the same dialog picker as the fields.

DatePickerField on iOS (left) and Android (right)TimePickerField on iOS (left) and Android (right)

Contents

Installation

npm install @nativescript/datetimepicker

Use @nativescript/datetimepicker

Core

  1. Register the plugin namespace with Page's xmlns attribute providing your prefix (datetime for example) and setting its value to the plugin name.

    <Page
      xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:datetime="@nativescript/datetimepicker"
    />
    
  2. Access the datePickerField view through the prefix.

    <Page
      xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:datetime="@nativescript/datetimepicker"
    >
      <datetime:DatePickerField hint="select date" />
      <datetime:TimePickerField hint="select time" />
      <datetime:DateTimePickerFields hintDate="select date" hintTime="select time" />
    </Page>
    

Angular

  1. Register the plugin by adding the plugin's NativeScriptDateTimePickerModule to the imports array of the module that contains the component where you want to use the DatePickerField:

    import { NativeScriptDateTimePickerModule } from '@nativescript/datetimepicker/angular'
    
    @NgModule({
      imports: [
        NativeScriptCommonModule,
        NativeScriptDateTimePickerModule,
        // ...
      ],
      // ...
    })
    
  2. Use DatePickerField in markup as follows:

    <DatePickerField hint="select date"></DatePickerField>
    <TimePickerField hint="select time"></TimePickerField>
    <DateTimePickerFields
      hintDate="select date"
      hintTime="select time"
    ></DateTimePickerFields>
    

Vue

  1. Once the plugin has been installed, register it with your app project, in the main.ts file for global access.

    import DateTimePicker from '@nativescript/datetimepicker/vue'
    
    import Home from './components/Home.vue'
    
    const app = createApp(Home).use(DateTimePicker)
    
    app.start()
    
  2. Use the DatePickerField in a template as follows:

    <DatePickerField hint="select date" />
    <TimePickerField hint="select time" />
    <DateTimePickerFields hintDate="select date" hintTime="select time" />
    

Svelte

Once you have installed the plugin, register the DateTimePickerFields with your app, in the app.ts file.

registerNativeViewElement(
  'dateTimePickerFields',
  () => require('@nativescript/datetimepicker').DateTimePickerFields
)

Next, use the picker in markup by adding the dateTimePickerFields element.

<dateTimePickerFields hintDate="pick a date" hintTime="pick time" />

Set the selected date and time

To set the selected time of the TimePickerField, use the time property.

<TimePickerField time="01:00" />

The DatePickerField and DateTimePickerFields components both use the date property to set their selected date(and the time for DateTimePickerFields) value.

<DatePickerField date="2019/02/24"></DatePickerField>

<DateTimePickerFields date="2019/02/24 01:00"></DateTimePickerFields>

Change the picker titles and buttons labels

To change the titles and the button labels of the picker's popups, use the pickerOkText, pickerCancelText and pickerTitle properties.

<DatePickerField
  hint="tap to choose"
  pickerOkText="Approve"
  pickerCancelText="Reject"
  pickerTitle="Confirm predefined date selection"
  pickerDefaultDate="2019/05/15"
/>

The DateTimePickerFields has the additional pickerTitleDate for the date picker title and the pickerTitleTime for the time picker title.

<DateTimePickerFields
  hintDate="tap to choose date"
  hintTime="tap to choose time"
  pickerOkText="Approve"
  pickerCancelText="Reject"
  pickerTitleDate="Confirm predefined date selection"
  pickerTitleTime="Confirm predefined time selection"
  pickerDefaultDate="2019/05/15 22:00"
  autoPickTime="true"
/>

Set localization

To localize the DatePickerField and DateTimePickerFields components, use the locale property.

<DatePickerField locale="en_GB" hint="select date"></DatePickerField>
<!-- DateTimePickerFields -->
<DateTimePickerFields
  locale="de_DE"
  hintDate="datum auswählen"
  hintTime="zeit wählen"
  pickerOkText="Bestätigen"
  pickerCancelText="Stornieren"
  pickerTitleDate="Datum auswählen"
  pickerTitleTime="Zeit wählen"
></DateTimePickerFields>

Format the date and time

Aside from the default formats that are dependent on the value of the locale property, you can add your custom format that can include ordering of the date/time values and also custom text. To customize the date format, use the dateFormat property.

<DatePickerField date="2019/02/24" dateFormat="'my date': dd-MMMM-yyyy" />

The TimePickerField will determine whether to use 12 or 24 hour format (for formatting of the selected time in the field and for the values of the hour spinner) based on the selected region in the settings of the iOS device and based on the Use 24-Hour Format setting of the Android device. To change the default setting on Android, you need to use the timeFormat property and to change the setting on iOS, you need to use the locale property.

<TimePickerField time="16:00" timeFormat="h:mm a" locale="en_US" />

For the DateTimePickerFields picker, use the same properties( dateFormat, timeFormat and locale) for custom date and time formats.

<DateTimePickerFields date="2019/02/24 16:00" timeFormat="h:mm a" locale="en_US" />

Set the minimum and maximum dates

To set the minimum and maximum dates, use the minDate and maxDate properties for the DatePickerField and DateTimePickerFields components.

<DatePickerField
  minDate="2020/02/02"
  maxDate="2021/02/02"
  hint="tap to select"
></DatePickerField>
<!-- DateTimePickerFields -->
<DateTimePickerFields
  minDate="2020/02/02"
  maxDate="2021/02/02"
  hintDate="tap to select date"
  hintTime="tap to select time"
></DateTimePickerFields>

Set the layout of DateTimePickerFields pickers

To lay out the picker fields in the horizontal(default) or vertical direction, use the orientation property.

<DateTimePickerFields
  hintDate="select date"
  hintTime="select time"
  orientation="vertical"
/>

Style the pickers with CSS

You can use CSS to style the DatePickerField and the TimePickerField pickers. The same styling applies to the pickers contained in DateTimePickerFields component. The picker supports changing its colors through the following predefined CSS classes:

  • date-time-picker: picker background and title text color)
  • date-time-picker-spinners: background and text color of the date/time selecting spinners
  • date-time-picker-buttons: background and text color of the OK/Cancel buttons
  • date-time-picker-button-ok and date-time-picker-button-cancel: to provide a separate style for each button.

Note

that the iOS native implementation has limited capabilities for the buttons background colors. When a button is marked as a Cancel button, its background is always white and can't be changed. If you really need a cancel button with another color, you can pass a background color through the designated cancel button class, however this will change the picker layout and place the cancel button along with the OK button and they will both have the same background color.

DatePickerField with CSS applied on iOS (left) and Android (right)
timepickerfield,
datepickerfield {
  padding: 12 4;
}

timepickerfield.apply-css,
datepickerfield.apply-css {
  color: #cddc39;
  background-color: #00796b;
  font-size: 20;
  font-weight: bold;
  padding: 20;
}

.date-time-picker.apply-css {
  color: #00796b;
  background-color: #cddc39;
}

.date-time-picker-spinners.apply-css {
  color: #cddc39;
  background-color: #00796b;
}

.date-time-picker-buttons.apply-css {
  color: #00796b;
}

To apply styles at runtime when opening the DateTimePicker you can do the following:

import { DateTimePicker, DateTimePickerStyle } from '@nativescript/datetimepicker'
import { Application, View } from '@nativescript/core'

function createPicker(args: EventData) {
  const dateTimePickerStyle = DateTimePickerStyle.create(args.object as any)

  // This example handles styling the calendar for light and dark mode of the device settings
  if (Application.systemAppearance() === 'dark') {
    // style for dark mode
    dateTimePickerStyle.buttonsBackgroundColor = new Color('#202125')
    dateTimePickerStyle.dialogBackgroundColor = new Color('#202125')
    dateTimePickerStyle.titleTextColor = new Color('#fff')
    dateTimePickerStyle.buttonsTextColor = new Color('#0067a6')
    dateTimePickerStyle.spinnersBackgroundColor = new Color('#202125')
    dateTimePickerStyle.spinnersTextColor = new Color('#fff')
  } else {
    // style for light mode
    dateTimePickerStyle.buttonsBackgroundColor = new Color('#fff')
    dateTimePickerStyle.dialogBackgroundColor = new Color('#fff')
    dateTimePickerStyle.titleTextColor = new Color('#0067a6')
    dateTimePickerStyle.buttonsTextColor = new Color('#0067a6')
    dateTimePickerStyle.spinnersBackgroundColor = new Color('#fff')
    dateTimePickerStyle.spinnersTextColor = new Color('#0067a6')
  }

  DateTimePicker.pickDate(
    {
      context: (args.object as View)._context,
      minDate: new Date((new Date() as any) - 1),
      title: 'DatePicker',
      okButtonText: 'Okay',
      cancelButtonText: 'Cancel',
      locale: 'es'
    },
    dateTimePickerStyle
  ).then(result => {
    // handle the result
  })
}

Create custom pickers manually

Internally DatePickerField and TimePickerField call the pickDate and pickTime methods of the DateTimePicker class which are public. You can create custom pickers by calling those methods manually. The pickDate method accepts DatePickerOptions, while the pickTime method accepts TimePickerOptions.

function onPickDateTap(args: EventData) {
  const dateToday = new Date()
  const dateTomorrow = new Date(
    dateToday.getFullYear(),
    dateToday.getMonth(),
    dateToday.getDate() + 1
  )
  const dateNextWeek = new Date(
    dateToday.getFullYear(),
    dateToday.getMonth(),
    dateToday.getDate() + 7
  )

  DateTimePicker.pickDate({
    context: args.object._context,
    date: dateTomorrow,
    minDate: dateTomorrow,
    maxDate: dateNextWeek,
    okButtonText: 'OK',
    cancelButtonText: 'Cancel',
    title: 'choose date',
    locale: 'en_GB'
  }).then(selectedDate => {
    if (selectedDate) {
      this.dateText = this.getFormattedDate(selectedDate)
    }
  })
}
function onPickTimeTap(args: EventData) {
  const dateToday = new Date()
  const dateTomorrow = new Date(
    dateToday.getFullYear(),
    dateToday.getMonth(),
    dateToday.getDate() + 1
  )
  dateTomorrow.setHours(8)
  dateTomorrow.setMinutes(0)
  DateTimePicker.pickTime({
    context: args.object._context,
    time: dateTomorrow,
    okButtonText: 'OK',
    cancelButtonText: 'Cancel',
    title: 'choose time',
    locale: 'en_GB',
    is24Hours: true
  }).then(selectedTime => {
    if (selectedTime) {
      this.timeText = this.getFormattedTime(selectedTime)
    }
  })
}

API

DatePickerField

PropertyTypeDescription
dateDateThe date the picker field is currently displaying. When used in markup, the provided string will be passed to the Date constructor to create a new Date object.
minDateDateThe minimum date the picker field can select.
maxDateDateThe maximum date the picker field can select.
localestringIdentifier of a locale that will be used to localize the names of the month names and also the order of the spinners (with en_GB first spinner is day, with en_US first spinner is month) (default is based on the device’s locale settings).
hintstringGets or sets the hint text. Hint is the text that is displayed in the field when date is null.
dateFormatstringFormat used for the text in the picker field (on android used as a pattern for a SimpleDateFormat, on iOS used as a dateFormat for NSDateFormatter, default is generated by the current value of the locale property).
pickerTitlestringText that will be displayed as title of the picker, default is undefined.
pickerOkTextstringText for the confirmation button of the picker (default is OK on iOS, localized version of OK on android (based on the devices locale settings)).
pickerCancelTextstringText for the cancel button of the picker (default is Cancel on iOS, localized version of Cancel on android (based on the devices locale settings)).

TimePickerField

PropertyTypeDescription
timeDateThe time the picker field is currently displaying. If it is in ISO 8601 format, it is passed to the JS Date constructor.
localestringGets or sets a locale for displaying the date in the field, and also for the picker. Default value is undefined, meaning that the format will be based on the device's settings.
hintstringGets or sets the hint text. Hint is the text that is displayed in the field when time is null.
timeFormatstringFormat used for the text in the picker field (on Android used as a pattern for a SimpleDateFormat, on iOS used as a dateFormat for NSDateFormatter, default is generated by the current value of the locale property), the format will also be used on Android to determine whether the picker will be in 12 or 24 hour format.
pickerTitlestringText that will be displayed as title of the picker, default is undefined.
pickerOkTextstringText for the confirmation button of the picker (default is OK on iOS, localized version of OK on android (based on the devices locale settings).
pickerCancelTextstringText for the cancel button of the picker (default is Cancel on iOS, localized version of Cancel on Android (based on the devices locale settings)).

DateTimePickerFields

PropertyTypeDescription
dateDateThe date the picker fields are currently displaying.
minDateDateGets or sets the min date. Note that this property only affects the date component.
maxDateDateGets or sets the max date. Note that this property only affects the date component.
localestringGets or sets a locale for displaying the date in the field, and also for the picker.
Default value is undefined, meaning that the format will be based on the device's settings.
Note that changing the locale will not affect the pickerOkText, pickerCancelText and pickerTitle or hint properties
dateFormatstringFormat used for the text in the picker field (on Android used as a pattern for a SimpleDateFormat, on iOS used as a dateFormat for NSDateFormatter, default is generated by the current value of the locale property).
timeFormatstringFormat used for the text in the picker field (on android used as a pattern for a SimpleDateFormat, on iOS used as a dateFormat for NSDateFormatter, default is generated by the current value of the locale property), the format will also be used on Android to determine whether the picker will be in 12 or 24 hour format.
pickerTitleDatestringText that will be displayed as title of the picker when the date component is tapped.
pickerTitleTimestringText that will be displayed as title of the picker, when the time component is tapped.
pickerOkTextstringText for the confirmation button of the picker (default is OK on iOS, localized version of OK on android (based on the devices locale settings)).
pickerCancelTextstringText for the Cancel button of the picker (default is Cancel on iOS, localized version of Cancel on Android (based on the devices locale settings)).
autoPickTimebooleanValue that indicates whether the time component should be assigned a value as soon as a date is picked by the date component. Defaults to false.
orientation'horizontal' | 'vertical'Value that indicates how the date and time components will be arranged, default is "horizontal", which means that they will be on the same row.
hintDatestringText displayed in the date component when date is null.
hintTimestringText displayed in the time component when date is null.

DateTimePicker Class

MethodReturn TypeDescription
pickDate(options: DatePickerOptions, style?: DateTimePickerStyle)Promise<Date>Picks a date from a dialog picker initialized with the provided options and styled with the optionally provided style.
pickTime(options: TimePickerOptions, style?: DateTimePickerStyle)Promise<Date>Picks a time from a dialog picker initialized with the provided options and styled with the optionally provided style.

DatePickerOptions

PropertyTypeDescription
contextanyView's context. Obtained from the _context property of a View instance.
dateDateOptional: The date that will be displayed in the picker, (if not provided, the picker will display today).
minDateDateOptional: The minimum date that can be selected.
maxDateDateOptional: The maximum date that can be selected.
localestringOptional: Identifier of a locale that will be used to localize month names and am/pm texts.
titlestringOptional: Text that will be displayed as title of the picker.
okButtonTextstringOptional: Text for the confirmation button of the picker (default is OK on iOS, localized version of OK on android (based on the devices locale settings)).
cancelButtonTextstringOptional: Text for the cancel button of the picker (default is Cancel on iOS, localized version of Cancel on android (based on the devices locale settings)).

TimePickerOptions

PropertyTypeDescription
contextanyView's context. Obtained from the _context property of a View instance.
timeDateOptional: The time that will be displayed in the picker, (if not provided, the picker will display now).
is24HoursbooleanOptional: This value will be used only on Android to determine whether the picker will be in 12 or 24 hour format.
localestringOptional: Identifier of a locale that will be used to localize month names and am/pm texts.
titlestringOptional: Text that will be displayed as title of the picker.
okButtonTextstringOptional: Text for the confirmation button of the picker (default is OK on iOS, localized version of OK on android (based on the devices locale settings)).
cancelButtonTextstringOptional: Text for the cancel button of the picker (default is Cancel on iOS, localized version of Cancel on android (based on the devices locale settings)).

DateTimePickerStyle

PropertyTypeDescription
titleTextColorColorColor to be used for the title text.
dialogBackgroundColorColorColor to be used as a background of the dialog picker.
spinnersTextColorColorColor to be used for the texts of the date/time spinners.
spinnersBackgroundColorColorColor to be used as a background of the date/time spinners.
buttonsTextColorColorColor to be used for the texts of the ok/cancel buttons.
buttonsBackgroundColorColorColor to be used as a background of the ok/cancel buttons.
buttonOkTextColorColorColor to be used for the texts of the ok button.
buttonOkBackgroundColorColorColor to be used as a background of the ok button.
buttonCancelTextColorColorColor to be used for the texts of the cancel button.
buttonCancelBackgroundColorColorColor to be used as a background of the cancel button.
create(view: View)DateTimePickerStyleCreates a style based on any css provided. The parameter is a View with the properly setup css class name.

License

Apache License Version 2.0