当解析存储的DateTime
并将其转换为DateTime.ToLocalTime()时,您是否指定“Utc”为DateTime.Kind?
public DateTime Submitted {
get {
DateTime utcTime = DateTime.SpecifyKind(DateTime.Parse(/*"Your Stored val from DB"*/), DateTimeKind.Utc);
return utcTime.ToLocalTime();
}
set {
...
}
}
^^为我工作得很好
更新:
class UtcToLocalDateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return DateTime.SpecifyKind(DateTime.Parse(value.ToString()), DateTimeKind.Utc).ToLocalTime();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
XAML:
<Window.Resources>
<local:UtcToLocalDateTimeConverter x:Key="UtcToLocalDateTimeConverter" />
</Window.Resources>
...
<TextBlock Text="{Binding Submitted, Converter={StaticResource UtcToLocalDateTimeConverter}}" />
来源
2013-06-04 15:39:21
Viv
从数据库加载日期时间时,我没有做任何事情,我刚刚使用EntityFramework ModelFirst实体。也许我需要重写该属性。 – BenCr
@BenCr好吧,你可以做同样的事情,我在转换器中的属性获得者。我会在一秒内编辑我的答案 – Viv
没有必要,我可以淘汰一个转换器,我只是不想沿着这条将它添加到每个绑定中的日期的路径。我希望XAML引擎中的某些内容可以根据当前的UI文化设置格式化日期。 – BenCr