This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

uidatepicker

Create date picker component

Syntax

d = uidatepicker
d = uidatepicker(Name,Value)
d = uidatepicker(parent)
d = uidatepicker(parent,Name,Value)

Description

d = uidatepicker creates a date picker in a new figure and returns the DatePicker object. MATLAB® calls the uifigure function to create the figure.

d = uidatepicker(Name,Value) specifies DatePicker property values using one or more Name,Value pair arguments.

d = uidatepicker(parent) creates a date picker in the specified parent container. The parent can be a Figure created using the uifigure function, or one of its child containers.

example

d = uidatepicker(parent,Name,Value) creates the date picker in the specified container and sets one or more DatePicker property values.

Examples

collapse all

Create a date picker in the upper left corner of a figure.

f = uifigure('Position',[500 500 320 280]);
d = uidatepicker(f,'Position',[18 235 150 22]);

Create a date picker that displays the date in the text field using the dd-MM-yyyy format. The watermark in the running app displays the new format, and all selected dates use that format.

f = uifigure('Position',[500 500 320 280]);
d = uidatepicker(f,'Position',[18 235 150 22]);
d.DisplayFormat = 'dd-MM-yyyy';

Create a date picker that disables Sundays and New Year's day 2018.

f = uifigure('Position',[500 500 375 280]);
d = uidatepicker(f,'Position',[18 225 150 22]);
d.DisabledDaysOfWeek = 1;
d.DisabledDates = datetime(2018,1,1);

When you expand the date picker and browse to January 2018, the first day of the year and all Sundays are disabled.

Create a program file called mydateapp.m that creates a figure and a date picker with a ValueChangedFcn callback.

function mydateapp
f = uifigure('Position',[340 400 415 300]);
d = uidatepicker(f,'DisplayFormat','MM-dd-yyyy',...
    'Position',[130 190 150 22],...
    'Value',datetime(2014,4,9),...
    'ValueChangedFcn', @datechange);

    function datechange (src,event)
        lastdate = char(event.PreviousValue);
        newdate = char(event.Value);
        msg = ['Change date from ' lastdate ' to ' newdate '?'];
        % Confirm new date
        selection = uiconfirm(f,msg,'Confirm Date');
        
        if (strcmp(selection,'Cancel'))
            % Revert to previous selection if cancelled
            d.Value = event.PreviousValue;
        end
    end
end

The datechange function displays a confirmation dialog box and determines which button the user clicks in that dialog box. The date picker reverts to the previous date if the user clicks Cancel.

Run the program, and click a date to see the confirmation dialog box.

mydateapp

Input Arguments

collapse all

Parent container, specified as a Figure created using the uifigure function, or one of its child containers: Tab, Panel, ButtonGroup, or GridLayout. If no container is specified, MATLAB calls the uifigure function to create a new Figure object that serves as the parent container.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: d = uidatepicker('Value',datetime('today')) creates a date picker with today's date selected in the UI.

Note

The properties listed here are only a subset. For a complete list, see DatePicker Properties.

Selected date, specified as a datetime object within the range of the Limits property. To make the selected date unspecified, set this property to NaT.

If the specified datetime object contains time information, only the date information is preserved in the Value property.

Example: d = uidatepicker('Value',datetime('today'))

Data Types: datetime

Display format for the date picker text field, specified as a character vector or string scalar. The default format depends on the locale of the system running the app.

The format you specify must use valid letter identifiers that correspond to the Unicode® Locale Data Markup Language (LDML) standard for dates and times. To separate fields, you can include nonletter characters such as a hyphen, space, colon, or any non-ASCII characters.

Example: d = uidatepicker('DisplayFormat','dd/MM/yy')

Examples of Common Formats

This table lists common display formats. The examples show formatted output for the date, Wednesday, April 9, 2014.

Value of FormatExample
'yyyy-MM-dd'2014-04-09
'dd/MM/yyyy'09/04/2014
'dd.MM.yyyy'09.04.2014
'yyyy年 MM月 dd日'2014年 04月 09日
'MMMM d, yyyy'April 9, 2014

All Date and Time Formats

Use these letter identifiers to create a display format. The third column of this table shows output for the date, Wednesday, April 9, 2014.

Letter IdentifierDescriptionDisplay
GEraCE
yYear, with no leading zeros.2014
yyYear, using last two digits.14
yyy, yyyy ...Year, using at least as many digits as there are instances of 'y'For the year 2014, 'yyy' displays 2014, while 'yyyyy' displays 02014.
u, uu, ...ISO year, a single number designating the year.2014
QQuarter, using one digit2
QQQuarter, using two digits02
QQQQuarter, abbreviatedQ2
QQQQQuarter, full name2nd quarter
MMonth, numerical, using one or two digits4
MMMonth, numerical, using two digits04
MMMMonth, abbreviated nameApr
MMMMMonth, full nameApril
MMMMMMonth, capitalized first letterA
WWeek of the month, using one digit2
dDay of the month, using one or two digits9
ddDay of the month, using two digits09
DDay of the year, using one, two, or three digits99
DDDay of the year, using two digits99
DDDDay of the year using three digits099
eDay of the week, numerical, using one or two digits4, where Sunday is the first day of the week
eeDay of the week, numerical, using two digits04
eeeDay, abbreviated nameWed
eeeeDay, full nameWednesday
eeeeeDay, capitalized first letterW

Note

  • The edit field in the running app accepts delimited numeric values, even when the DisplayFormat includes words. For instance, if the month format is specified as 'MMMM', the app accepts a numeric month such as 04, but will display a month name such as 'April'.

  • If the user specifies a day-of-year number in the running app, and the format contains identifiers for both the day of year (D) and Gregorian year (y), then datetime might not read the day-of-year number correctly. Use ISO year (u) in place of y.

  • Use one or more u characters instead of y characters to represent the year when working with year numbers near zero.

Value changed function, specified as one of the following:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

The ValueChangedFcn callback executes when the user changes the date by typing in the text field or by expanding the date picker and selecting a date.

This callback function can access specific information about the user’s interaction with the date picker. MATLAB passes this information in a ValueChangedData object as the second argument to your callback function. In App Designer, the argument is called event. You can get the object properties using dot notation. For example, event.PreviousValue gets the previously selected date. The ValueChangedData object is not available to callback functions specified as character vectors.

The following table lists the properties of the ValueChangedData object.

PropertyValue
ValueNew selected date
PreviousValuePreviously selected date
SourceComponent that executes the callback
EventName'ValueChanged'

The ValueChangedFcn callback does not execute when the user re-selects or re-types the currently selected date. The callback also does not execute when the Value property changes programmatically.

For more information about creating callbacks in App Designer, see Write Callbacks in App Designer.

Location and size of the collapsed date picker relative to the parent container, specified as a vector of the form [left bottom width height]. This table describes each element in the vector.

ElementDescription
leftDistance from the inner left edge of the parent container to the outer left edge of the date picker
bottomDistance from the inner bottom edge of the parent container to the outer bottom edge of the date picker
widthDistance between the right and left outer edges of the date picker
heightDistance between the top and bottom outer edges of the date picker

All measurements are in pixel units.

See Also

Functions

Properties

Introduced in R2018a