Hide day in DatePicker

Hi, is there a way to hide the day field in a DatePicker? I tried using different values for the Format property but it doesn't seem to have any effect.

Answers

  • WesPeter.8925WesPeter.8925 USMember

    I actually realized that using Format does work in Android but doesn't seem to work on WinRT

  • RendyDelRosarioRendyDelRosario ✭✭✭ DOUniversity ✭✭✭

    You can try using the TimePicker instead:

    iosapi.xamarin.com/?link=T%3aXamarin.Forms.TimePicker%2f*

  • WesPeter.8925WesPeter.8925 USMember

    I still want to pick a date, not a time. What I'm trying to accomplish here is having a user input their credit card information which takes an expiration month and year (not day).

  • Omer.7911Omer.7911 ✭✭ USMember ✭✭

    Any update on this issue?

  • YkshLeoYkshLeo ✭✭✭✭ USMember ✭✭✭✭
  • Omer.7911Omer.7911 ✭✭ USMember ✭✭

    For XF?

  • YkshLeoYkshLeo ✭✭✭✭ USMember ✭✭✭✭

    I didn't find any XF samples or posts on this topic.

    I think we must do something render to achieve this.

  • mateusofmateusof USMember

    Any update?

  • ZainSMJZainSMJ ✭✭ USMember ✭✭

    Any update ?

  • JohnTolleJohnTolle ✭✭ USMember ✭✭

    For Windows UWP (not sure if it works on 8.1), you can do something like this:
    https://forums.xamarin.com/discussion/68507/datepicker-timepicker-width-and-rendering-problems-on-universal-windows

    But change:
    SetNativeControl(new Windows.UI.Xaml.Controls.DatePicker { MinWidth = 0 });

    To:
    SetNativeControl(new Windows.UI.Xaml.Controls.DatePicker { DayVisible = false });

    There are likely equivalents for Android and iOS. If you haven't done custom renderers before, check out the docs:
    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/introduction/

    Or perhaps this site:
    http://www.wintellect.com/devcenter/jprosise/supercharging-xamarin-forms-with-custom-renderers-part-1

    Now if someone can tell me how to change the actual formatting in a DatePicker on any Windows platform, I'll be giddy (because I get ugly exceptions when I try setting Windows.UI.Xaml.Controls.DatePicker.DayFormat, etc.)

  • SwathiSudarSwathiSudar ✭✭ USMember ✭✭

    Date Picker:
    DatePicker date_picker = new DatePicker()
    {
    Format = "yyyy'-'MM'-'dd",
    MaximumDate=DateTime.Now,
    MinimumDate=DateTime.Now.AddDays(-7)

            };
    
  • tdkeentdkeen ✭✭ Member ✭✭

    This is old but I thought I'd add some info just in case someone else could use it.

    This is only for UWP.

    Put the following in your UWP App.Xaml file. The below is just an example. The formats that you can use come up in intellisense(for Visual Studio at least).

     <Style TargetType="DatePicker">
                 <Setter Property="MonthFormat" Value="{}{month.integer} {month.abbreviated}"/>
                 <Setter Property="DayVisible" Value="False"/>
     </Style>
    
  • LeoJHarrisLeoJHarris ✭✭✭ NZMember ✭✭✭
    edited May 7

    Android only using a renderer but correctly removes the day field in a picker

    Share Project:

    public class EnhancedDatePicker : Picker
        {
        }
    

    Android Renderer:

    [assembly: ExportRenderer(typeof(EnhancedDatePicker), typeof(EnhancedDateTimePickerRenderer))]
    namespace YourNameSpace
    {
        public class EnhancedDateTimePickerRenderer : PickerRenderer
        {
            private readonly Context _context;
    
            public EnhancedDateTimePickerRenderer(Context context) : base(context)
            {
                _context = context;
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Picker> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    this.Control.FocusChange += onPickerFocusChange;
                }
            }
    
            private void showDialogWithoutDayDateField()
            {
                int MAX_YEAR = DateTime.Now.AddYears(5).Year;
    
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.Instance);
    
                LayoutInflater inflater = MainActivity.Instance.LayoutInflater;
    
                Java.Util.Calendar cal = Java.Util.Calendar.Instance;
    
                Android.Views.View dialog = inflater.Inflate(Resource.Drawable.date_picker_dialog, null);
                NumberPicker monthPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_month);
                NumberPicker yearPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_year);
    
                monthPicker.MinValue = 1;
                monthPicker.MaxValue = 12;
                monthPicker.Value = cal.Get(Java.Util.CalendarField.Month) + 1;
    
                int year = cal.Get(Java.Util.CalendarField.Year);
                yearPicker.MinValue = year;
                yearPicker.MaxValue = MAX_YEAR;
                yearPicker.Value = year;
    
                builder.SetView(dialog)
                    .SetPositiveButton("Ok", (sender, e) =>
                    {
                        Control.Text = new DateTime(yearPicker.Value, monthPicker.Value, 1).ToString("MM/yy");
                    })
                    .SetNegativeButton("Cancel", (sender, e) => { });
    
                builder.SetOnDismissListener(new OnDismissListener(() =>
                {
                    Control.ClearFocus();
                }));
    
                builder.Create().Show();
            }
    
            private void onPickerFocusChange(object sender, FocusChangeEventArgs e)
            {
                if (e.HasFocus)
                {
                    showDialogWithoutDayDateField();
                }
            }
    
            private sealed class OnDismissListener : Java.Lang.Object, IDialogInterfaceOnDismissListener
            {
                private readonly Action _action;
    
                public OnDismissListener(Action action)
                {
                    this._action = action;
                }
    
                public void OnDismiss(IDialogInterface dialog)
                {
                    this._action();
                }
            }
        }
    }
    
  • LeoJHarrisLeoJHarris ✭✭✭ NZMember ✭✭✭
    edited May 9

    Forgot to add the date_picker_dialog.axml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">
    
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:orientation="horizontal">
    
        <NumberPicker
            android:id="@+id/picker_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp">
        </NumberPicker>
    
        <NumberPicker
            android:id="@+id/picker_year"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </NumberPicker>
      </LinearLayout>
    </LinearLayout>
    
Sign In or Register to comment.