UIDatePicker: setDate() method
The setDate() method changes the moment selected in the picker, optionally animated. The only difference from writing date is the animation.
Syntax
setDate(date[, { animated }])Parameters
date
The moment to select, a Date. Strings and timestamps aren't converted. A value earlier than minimumDate or later than maximumDate doesn't throw; it is changed to the nearest limit.
options.animated optional
A boolean. true: in the wheels style, the wheels spin to the new value. If omitted, there is no animation.
Return value
None (undefined).
When animated, date already has the new value when the method returns.
Doesn't fire change.
Exceptions
TypeError
dateisn't aDate:setDate must be a Date, got 1700000000000.dateis an invalidDate:setDate must be a valid Date, got an invalid one.
After it throws, the selected moment is unchanged.
Examples
const picker = new UIDatePicker();
picker.preferredDatePickerStyle = 'wheels';
picker.setDate(new Date(2027, 0, 1), { animated: true });
console.log(picker.date.getFullYear());Effect: Prints 2027. The wheels spin to January 1, 2027. The wheels spinning is not verified in the simulator.
Corresponding iOS API
-[UIDatePicker setDate:animated:]. animated is a required parameter in native; here it can be omitted.