I have been writing a WP7 app and part of the functionality involves the use of a DateTime value. This value is also stored (in a Sterling database).
I have tried to steer away from different culture values in the dates and where I need to present it I do so using my own format out. I never 'fiddle' with DateTime as such.
All works fine with the emulator, on a PC which is setup running the English (New Zealand Locale)
But on the phone itself, things were being screwed up as the the day and month were being screwed up. Have now realised that the locale on the phone was English (United States) but the region format was English (New Zealand). Have changed both to be English (United States) and all is fine on the phone.
So my question(s) are
1/ should I need to allow for differing locales and regions
2/ Is there an easy way to ensure that this issue does not occur?
My code is as follows
A property defined as follows
public const string NextDateTimePropertyName = "NextDateTime";
private DateTime _nextDateTime;
public DateTime NextDateTime
{
get
{
return _nextDateTime;
}
set
{
if (_nextDateTime != value)
{
_nextDateTime = value;
RaisePropertyChanged(NextDateTimePropertyName);
}
}
}
A binding to display the date as follows TextBlock Text="{Binding DateTimeDayString}"
and a property on my class hat maps to the binding
public string DateTimeDayString
{
get
{
return NextDateTime.ToString("dddd MMM d");
}
}
When the Phone locale and region are the same country everything works fine, however when the Locale and region are different i.e. Locale English - UK and English - US then the 9th August as entered would display as the "Thursday, 8th September"
I realise that having a differing locale and region is an unusual setup.. but was looking to see how I could protect myself against this.
Date is being selected via a DatePicker control
x:Name="datePicker" Value="{Binding EventDate, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
ValueChanged="datePicker_ValueChanged" ValueStringFormat="{}{0:D}" Margin="22,87,91,0"
with a property in the view model as follows
private DateTime _EventDateTime;
public DateTime EventDateTime
{
get
{
return _EventDateTime;
}
set
{
if (value != null)
{
_EventDateTime = value;
}
}
}
And when I store this property
CurrSingleEventItem.NextDateTime = EventDate.BuildDateTime(EventTime);
And the BuildDateTime Extension method (because I am having the user enter the time via a timepicker as well
public static DateTime BuildDateTime(this String DateString, String time)
{
DateTime dt = System.Convert.ToDateTime(DateString);
DateTime timedt = System.Convert.ToDateTime(time);
string timestr = timedt.ToString("H:mm");
DateTime newDt = System.Convert.ToDateTime(dt.ToLongDateString() + " " + timestr + ":00");
return newDt;
}
- thanks - Peter