uidatepicker
Create date picker component
Syntax
Description
creates a date picker
in a new figure and returns the d
= uidatepickerDatePicker
object. MATLAB® calls the uifigure
function to create the
figure.
specifies d
= uidatepicker(Name,Value
)DatePicker
property values using one or more
Name,Value
pair arguments.
creates the date picker in the specified container and sets one or more
d
= uidatepicker(parent
,Name,Value
)DatePicker
property values.
Examples
Create a Date Picker in a UI Figure
Create a date picker in the upper left corner of a UI figure.
fig = uifigure('Position',[500 500 320 280]); d = uidatepicker(fig,'Position',[18 235 150 22]);
Display Custom Date Format
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.
fig = uifigure('Position',[500 500 320 280]); d = uidatepicker(fig,'Position',[18 235 150 22]); d.DisplayFormat = 'dd-MM-yyyy';
Disable Sundays and a Holiday
Create a date picker that disables Sundays and New Year's day 2018.
fig = uifigure('Position',[500 500 375 280]); d = uidatepicker(fig,'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 Callback for Date Selection
Create a program file called mydateapp.m
that creates a
figure and a date picker with a ValueChangedFcn
callback.
function mydateapp fig = uifigure('Position',[340 400 415 300]); d = uidatepicker(fig,'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(fig,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
parent
— Parent container
Figure
object (default) | Tab
object | Panel
object | ButtonGroup
object | GridLayout
object
Parent container, specified as a Figure
object created using the uifigure
function or one of its child
containers: Tab
, Panel
, ButtonGroup
, or GridLayout
. If you do not specify a parent container, MATLAB calls the uifigure
function to create a new Figure
object that serves as the parent container.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
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
.
Value
— Selected date
NaT
(default) | datetime
object
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
DisplayFormat
— Display format
character vector | string scalar
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 Format | Example |
---|---|
'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 Identifier | Description | Display |
---|---|---|
G | Era | CE |
y | Year, with no leading zeros. | 2014 |
yy | Year, 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 |
Q | Quarter, using one digit | 2 |
QQ | Quarter, using two digits | 02 |
QQQ | Quarter, abbreviated | Q2 |
QQQQ | Quarter, full name | 2nd quarter |
M | Month, numerical, using one or two digits | 4 |
MM | Month, numerical, using two digits | 04 |
MMM | Month, abbreviated name | Apr |
MMMM | Month, full name | April |
MMMMM | Month, capitalized first letter | A |
W | Week of the month, using one digit | 2 |
d | Day of the month, using one or two digits | 9 |
dd | Day of the month, using two digits | 09 |
D | Day of the year, using one, two, or three digits | 99 |
DD | Day of the year, using two digits | 99 |
DDD | Day of the year using three digits | 099
|
e | Day of the week, numerical, using one or two digits | 4 , where Sunday is the first day of the
week |
ee | Day of the week, numerical, using two digits | 04 |
eee | Day, abbreviated name | Wed |
eeee | Day, full name | Wednesday |
eeeee | Day, capitalized first letter | W |
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 as04
, 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
), thendatetime
might not read the day-of-year number correctly. Use ISO year (u
) in place ofy
.Use one or more
u
characters instead ofy
characters to represent the year when working with year numbers near zero.
ValueChangedFcn
— Value changed function
''
(default) | function handle | cell array | character vector
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.
Property | Value |
---|---|
Value | New selected date |
PreviousValue | Previously selected date |
Source | Component 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 Callbacks in App Designer.
Position
— Location and size
[100 100 150 22]
(default) | [left bottom width height]
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.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the date picker |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the date picker |
width | Distance between the right and left outer edges of the date picker |
height | Distance between the top and bottom outer edges of the date picker |
All measurements are in pixel units.
Version History
Introduced in R2018aR2021a: Specify placeholder text
Provide a short hint that describes the expected date picker input by using the
Placeholder
property.
For more information, see DatePicker
.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)