Xamarin.Forms default DatePicker value?

Viewed 13670

How can I set a default or starting value for a Xamarin.Forms DatePicker?

At the moment, it begins on January 1st, 1900. This isn't a very useful date, and worse, a user has to scroll needlessly to get the right year, then the right month.

I'd like the default date to be the current date. I know how to get the current date, and I can set MinimumDate attribute of the DatePicker tag in the .xaml. This will make the current date the start date of the picker, but then I can't go back to a previous year or month.

Thanks,

3 Answers

You can just set Date property directly on xaml:

<DatePicker x:Name="datePicker" Date="{x:Static system:DateTime.Today}" />

you need to use

xmlns:system="clr-namespace:System;assembly=mscorlib"

2020 update: the default value is now the current date. If you want to change it to a different value you can do it in the xaml by using the Date attribute, as noted by Giancarlo. You can pass in a string like this:

<DatePicker Date="06/13/2000"/>

Try this code:

< DatePickerVariable.Date = DateTime.Now />
Related