wpf 如何获得特定于文化的短日期字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11665696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:44:03  来源:igfitidea点击:

How do I get a culture specific short date string?

c#wpf

提问by Sachin

I want short date with culture format. I got date with the culture but also want only date, not a time. I am using IValueConverterfor that.

我想要文化格式的短日期。我与文化约会,但也只想要约会,而不是时间。我正在使用IValueConverter它。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var item = (DateTime)value;

    if (item != null)
    {
        return item.ToString(culture);
    }

    return null;
}

回答by Arif Eqbal

This will give you based on current culture, if you want a specific culture that can be done too...

如果您想要一种也可以完成的特定文化,这将为您提供基于当前文化的信息...

string format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
String Date = DateTime.Now.ToString(format);

回答by Sachin

Your answer is also right but I found the solution thanks.

您的回答也是正确的,但我找到了解决方案,谢谢。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var item = (DateTime)value;
    if (item != null)
    {
        return item.ToString(culture.DateTimeFormat.ShortDatePattern, culture);
    }
    return null;
}