My application synchronises data across several different devices. For this reason it stores all dates in the UTC time-zone to account for different devices possibly being set to different time zones.
The trouble is that when I read the dates back out and display them they appear to be incorrect (most of the users are on British Summer Time so they're an hour behind).
<TextBlock Margin="5" Style="{StaticResource SmallTextblockStyle}">
<Run Text="Last Updated:" />
<Run Text="{Binding Path=Submitted}" />
</TextBlock>
Do I need to manually override set CurrentCulture property of the UI thread? I know I have to do this in Silverlight.
Are you specifying "Utc" as DateTime.Kind when parsing the stored DateTime
and also converting it to DateTime.ToLocalTime()?
public DateTime Submitted {
get {
DateTime utcTime = DateTime.SpecifyKind(DateTime.Parse(/*"Your Stored val from DB"*/), DateTimeKind.Utc);
return utcTime.ToLocalTime();
}
set {
...
}
}
^^ works fine for me
Update:
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}}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With