How to format date and time in XAML in Xamarin application

前端 未结 4 2119
时光取名叫无心
时光取名叫无心 2020-12-08 13:52

I have a set up XAML code below.



4条回答
  •  死守一世寂寞
    2020-12-08 14:13

    Make a custom IValueConverter implementation:

    public class DatetimeToStringConverter : IValueConverter
    {
        #region IValueConverter implementation
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;
    
            var datetime = (DateTime)value;
            //put your custom formatting here
            return datetime.ToLocalTime().ToString("g");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException(); 
        }
    
        #endregion
    }
    

    Then use it like that:

    
        
    
    
    ...
    
    
    
    

提交回复
热议问题