I'm working on Universal Store app. I've made this converter:
public object Convert(object value, Type targetType, object parameter, string language)
{
string stringDate = (string)value;
IFormatProvider culture = new CultureInfo( language );
DateTime date = DateTime.Parse(stringDate, culture);
return date.LongTimeString();
}
but Method LongTimeString is not recognized. This method is deprecated? Any solution? Thanks
DateTime doesn't have a method as LongTimeString. I think you looking for ToLongTimeString method.
return date.ToLongTimeString();
Mabe you are mixing this method with LongTimePattern property of DateTimeFormatInfo.
It's not deprecated, just not part of the WinRT API.
The solution is something like
return date.ToString("D");
But I'm not sure what exactly is supported.
Related
This question already has an answer here:
WPF binding StringFormat syntax
(1 answer)
Closed 9 years ago.
I have a DataGridthat has a Start Time column. This is bound to a string field which holds the time in the format hh:mm:ss. However, I want to only display the time in the grid in the format hh:mm. Is there a way of achieving this using the string format attribute in Binding.
like this:
<DataGridTextColumn Binding="{Binding MyDate, StringFormat='HH:mm'}" />
I've exhausted my search for trying to find out if there was a way for the StringFormat attribute of Binding in WPF to convert a string to a date and then format it as HH:mm.
So I've gone about creating a converter to do the same as suggested above. Thought I'd paste it here for use by anyone who has such a need.
public class StringToDateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return string.Empty;
}
DateTime d;
try
{
d = System.Convert.ToDateTime(value);
return d;
// The WPF code that uses this converter will then use the stringformat
// attribute in binding to display just HH:mm part of the date as required.
}
catch (Exception)
{
// If we're unable to convert the value, then best send the value as is to the UI.
return value;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//Insert your implementation here...
return value;
}
}
And here is how you achieve the result in WPF.
<DataGridTextColumn Header="Start Time" Width="Auto" Binding="{Binding VisitOpStartTime, Converter={StaticResource s2dConverter}, StringFormat=\{0:HH:mm\}}"></DataGridTextColumn>
Lastly, don't forget to add the following, customised ofcourse to your needs.
xmlns:converter="clr-namespace:[YourAppNamespace].Converters"
<Window.Resources>
<converter:StringToDateConverter x:Key="s2dConverter" />
</Window.Resources>
Hope this helps.
I have this converter that i made to get the current time once the date is selected from the DataPicker. In string Date i am getting the value that was selected from the DatePicker, but i cant seem to only get the date. The format that is coming into the Value property is 9/24/2013 12:00:00 I would like it to be 9/24/2013
the error i am getting is "Error 122 No overload for method 'ToString' takes 1 argument"
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
string date = value.ToString("d/M/yyyy");
return (date);
}
return string.Empty;
}
You need to cast it to DateTime first:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
DateTime test = (DateTime) value;
string date = test.ToString("d/M/yyyy");
return date;
}
return string.Empty;
}
You should cast value to DateTime type, because there is not a ToString(String f) method for the type of Object.
if (value is DateTime)
{
var dateTime = (DateTime)value;
return dateTime.ToString("dd/MM/yyyy");
}
return string.Empty;
After check on type of value you need cast it to appropriate type, to be able to perform "ToString" call with format parameter. Try:
if (value is DateTime)
{
var dateValue = value as DateTime;
string date = dateValue.ToString("dd/MM/yyyy");
return date;
}
If you are using a Converter on a WPF DatePicker control, you should note that the WPF DatePicker will itself reformat the date despite the converter that you use. You will have to style the Datepicker to include a StringFormat.
There is a related question here: how to show just Month Year string format in a DatePicker which shows an attached property to modify the behaviour of DatePicker to display a custom format. This is needed because of a deficiency in the WPF Datepicker control itself.
Also note there are some caveats, notably the DatePicker will flicker between its default stringformat and the one you apply! I answered in the above question a workaround for to apply a custom Format to WPF Datepicker without the flicker.
Hope you find the solution you are looking for.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
string date=value.Date.ToShortDateString();
return (date);
}
return string.Empty;
}
I am calling a IValueConverter class via code behind, but I am not sure what to put in the Type targetType parameter. The object is string but using that give me 'invalid expression term 'string'`
my code to call the converter
secondConverter.Convert(score, string, null, CultureInfo.CurrentCulture);
The converter class
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
TimeSpan ts = new TimeSpan(0, 0, (int)value);
return String.Format("{0:D2}:{1:D2}:{2:D2}",
ts.Hours,
ts.Minutes,
ts.Seconds);
}
You can put typeof(string) instead of string, but your converter does not seem to use or validate target type, so you can put just about anything there, including null.
Generally, your converter should at least validate that target type is string and throw exception if it is not.
You'll want
secondConverter.Convert(score, typeof(string), null, CultureInfo.CurrentCulture);
to actually make it a parameter of type Type.
i am converting strings to values using a culture specified to me as a IFormatProvider.
i am trying to figure out which culture they gave me.
i realize that IFormatProvider doesn't necessarily have to correspond to a System.Globalization.Culture, but it did.
So how can i get its name?
The CultureInfo class implements IFormatProvider so you may try casting:
IFormatProvider provider = ...
CultureInfo ci = provider as CultureInfo;
if (ci != null)
{
string name = ci.DisplayName;
...
}
One general question:
Does the DateTime object stores the CultureInfo with it, or you need to use the Formatter to format the DateTime according to current culture ?
I have a class property that retuns a DateTime. Within that property I am setting the DateTime object with current culture information using CultureInfo object. Below is the code for class property I am using:
public DateTime PrintedQuoteDate {
get {
DateTime printQuoteDate = DateTime.Today;
// cInfo = CultureInfo object
return Convert.ToDateTime(printQuoteDate , cInfo);
}
}
So my question is when I will use the above property in my code, will it have the corrosponding culture information that I am setting in its get method, or I will have to use the same CONVERT code for formatting date time. The restriction here is that the Property should return only DateTime type.
Any idea, suggestions
DateTime does not store any Culture what so ever. In fact it does not even hold a reference to a TimeZone, all it knows is whether it is a UTC DateTime or not. This is handled by an internal enum.
You need to specify a format provider (every culture in itself is a format provider) when using the ToString method of a DateTime, otherwise it will use the culture (really the culture and not the UI Culture) of the current thread.
You can get a predifined culture by using the ISO country/locale codes like this:
var us = new CultureInfo("en-US");
var british = new CultureInfo("en-GB");
var danish = new CultureInfo("da");
As you can see for danish it is enough to specify the language since there are no other locales (to my knowledge).