आप स्वतंत्र भाषा रहने के लिए चाहते हैं, आप निम्न कोड का उपयोग कर सकते हैं (शायद IValueConverter में (दूसरा कोड स्निपेट देखें)) स्ट्रिंग से केवल सेकंड भाग को निकालने के लिए:
int index = dateTimeString.LastIndexOf(':');
if (index > -1) {
dateTimeString = dateTimeString.Remove(index, 3);
}
यहां कनवर्टर का कार्यान्वयन है।
[ValueConversion(typeof(DateTime), typeof(string))]
public class DateTimeToStringConverter : Markup.MarkupExtension, IValueConverter {
public DateTimeToStringConverter() : base() {
DisplayStyle = Kind.DateAndTime;
DisplaySeconds = true;
}
#region IValueConverter
public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture) {
if (value == null) return string.Empty;
if (!value is DateTime) throw new ArgumentException("The value's type has to be DateTime.", "value");
DateTime dateTime = (DateTime)value;
string returnValue = string.Empty;
switch (DisplayStyle) {
case Kind.Date:
returnValue = dateTime.ToShortDateString();
break;
case Kind.Time:
returnValue = dateTime.ToLongTimeString();
break;
case Kind.DateAndTime:
returnValue = dateTime.ToString();
break;
}
if (!DisplaySeconds) {
int index = returnValue.LastIndexOf(':');
if (index > -1) {
returnValue = returnValue.Remove(index, 3);
}
}
return returnValue;
}
public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture) {
throw new NotSupportedException();
}
#endregion
public override object ProvideValue(IServiceProvider serviceProvider) {
return this;
}
#region Properties
public Kind DisplayStyle { get; set; }
public bool DisplaySeconds { get; set; }
#endregion
public enum Kind {
Date,
Time,
DateAndTime
}
}
तुम भी एक मार्कअप विस्तार के रूप में XAML में इसका इस्तेमाल कर सकते हैं:
<TextBlock Text="{Binding CreationTimestamp, Converter={local:DateTimeToStringConverter DisplayStyle=DateAndTime, DisplaySeconds=False}}" />
कृपया यह http://www.csharp-examples.net/string-format-datetime/ –
@Amit, क्या आप इसके लिए उत्तर चिह्नित कर सकते हैं? – JohnOsborne