-1

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
Peter
  • 679
  • 1
  • 10
  • 25

1 Answers1

0

You haven't really said what was wrong - "the day and month were being screwed up" isn't very precise. If the region format was set to New Zealand and you were seeing dates formatted as dd/MM/yyyy, that seems entirely reasonably to me - it's what someone from New Zealand (or the UK, or most places other than the US...) would probably expect.

As for whether you need to allow for different locales and regions - that's entirely your business. Are you expecting the app to be used in multiple countries? If so, then you absolutely should be culture-sensitive.

As for your second question: it's not at all clear that there was an "issue" in the first place. If the date was simply formatted in a New Zealand style, I don't see that as an issue. If you ended up with corrupt data somehow, that's a different matter - and one you need to give far more details of.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ok The issue was that the day and month were being transposed. My question re allowing for different locales and regions .... I have deliberately not relied upon the date format within the app and always present it how I want but do not rely upon this as the data being stored. I just store a date in a DateTime variable, which I assume would handle things. And perhaps what I should have said re handling differing locales and regions was if the phone has a different locale to its region. – Peter Aug 13 '11 at 07:38
  • @Peter: You say they were being transposed - but what would a *user* want? You should format it how the *user* wants, not how *you* want. If you don't have any users in different cultures, that's fine - but otherwise you need to consider how different cultures format dates. `DateTime` itself doesn't have any formatting information, so you won't be losing information. Formatting is handled by the `DateFormatInfo` associated with the culture. If you really want to force a particular format, you can do so of course. – Jon Skeet Aug 13 '11 at 07:52
  • Jon... the format that I am putting is is culture independent. Its a slightly different format that is more generic that dd/mm/yy as opposed to mm/dd/yy. I ama ware of the different cultures dates. I have no problem formatting the date that I put out. I am very conscious of the culture specific stuff. I had expected that DateTime would work as you suggest (no formatting info) and therefore I would be okay as long as I was aware of how I presented my date. What I have discovered is that when the phone is set with a different Local to the Region then its does not work as I expected. – Peter Aug 13 '11 at 11:34
  • When the Locale and Region match then everything works fine. When they differ then I get the issues. – Peter Aug 13 '11 at 11:35
  • @Peter: If you want to use a culture-independent format, then just specify it explicitly. You still haven't really explained what you're trying to do, or what's happening at the moment... if you could show some code, the actual output and the expected output, that would really help... – Jon Skeet Aug 13 '11 at 11:48
  • @Peter: Okay, it sounds like it's a data *input* problem - how are you getting the data to start with? – Jon Skeet Aug 14 '11 at 07:59
  • Ok - have now included the code to get and save it. Perhaps there is a way that the separate datepicker and timepicker can work with the same property and avoid the need for me to fiddle with like i currently do?? – Peter Aug 14 '11 at 08:17
  • @Peter: Okay, so this is probably the problem: `DateTime dt = System.Convert.ToDateTime(DateString);` - that's parsing with the default settings. Fundamentally you shouldn't be doing all this parsing at all - you should use the `SelectedDate` of the `DatePicker` without going through strings. The more string formatting and parsing you do, the more chance there is for error. It's easy enough to combine two `DateTime` values using new `DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute etc)` - using strings here is a really bad idea. – Jon Skeet Aug 14 '11 at 08:30
  • Ok - all good now - no fiddling strings but using the properties from DateTime as suggested. Thanks! – Peter Aug 15 '11 at 06:51