String value can not be converted to a date. SmartDate Culture?

String value can not be converted to a date. SmartDate Culture?

Old forum URL: forums.lhotka.net/forums/t/10384.aspx


ddredstar posted on Wednesday, May 18, 2011

private static PropertyInfo<SmartDate> CreatedOnProperty = RegisterProperty<SmartDate>(p => p.CreatedOn);
    public string CreatedOn
    {
        get { return GetPropertyConvert<SmartDate, string>(CreatedOnProperty); }
        set { SetPropertyConvert<SmartDate, string>(CreatedOnProperty, value); }
    }

 

The value is   5/18/2011 12:00:00 AM

 

Error happens:

Property load or set failed for property CreatedOn (String value can not be converted to a date)

 

How to resolve this?

ddredstar replied on Wednesday, May 18, 2011

After change my computer's Region Option from English(United Kingdom) to English(United English), it's works well.

 

I think the SmartDate has some inprove space, look forward you advices !!!

JonnyBee replied on Wednesday, May 18, 2011

SmartDate uses the default DateTime.TryParse method to convert from string to datetime value.

Are you using non-default format string on your SmartDate properties?

 

 

 

ddredstar replied on Wednesday, May 18, 2011

The string is   5/18/2011 12:00:00 AM

is this a wrong format ?

 

if the Region Option of my computer is English(United States) it works fine, if i change my Region Option to English(United Kingdom) , the error occurs!!!

RockfordLhotka replied on Wednesday, May 18, 2011

Wouldn't the UK date be 18/5/2011?

ddredstar replied on Wednesday, May 18, 2011

a user in UK, whose date format is 18/5/2011, encountered this error, but we didn't .

RockfordLhotka replied on Thursday, May 19, 2011

I recommend creating a simpler test that uses DateTime.TryParse and/or SmartDate.TryParse to see if you can isolate the issue.

The SmartDate parse function uses the DateTime parse function to do the actual parsing, so the real question is whether DateTime.TryParse can parse the input given the configuration on your computer.

You should absolutely expect that "5/18/2011" will not parse if the UK culture is set, because that's not a valid UK date.

The thing is, Windows and .NET both have several culture settings. I don't know which one(s) affect date parsing. I suspect a machine in the UK has all of them set to the UK culture. But your machine or my machine (not in the UK) would only have some of the culture settings changed.

In .NET alone there are at least 2 culture settings - one for the thread, and one for the UI - and those can be independent of the Windows OS setting(s).

I'm not an expert in this area, so I don't know what they all do to be honest. Jonny knows more about it than I do, so maybe he can help make suggestions.

JonnyBee replied on Thursday, May 19, 2011

Users tend to do strange things!!!.

DateTime.TryParse will use the System.Threading.CurrentCulture to get the formatting info.

The default values for the "default culture" may however be overridden by the user in "Regional and language settings" .

So from my own experience - a user running norwegian OS and norwegian default culture may change the regional settings to use specific formats like UK (english).
This will change DateTime.ToString and DateTime.Parse/TryParse to use the specific formats.

Using LinqPad  (available from www.linqpad.net) to execute the following C# Statements:

var date = DateTime.Now; 
date.ToString("d").Dump();   // using the default culture
Thread.CurrentThread.CurrentCulture =
   System.Globalization.CultureInfo.GetCultureInfo("nb-NO");
date.ToString("d").Dump();
Thread.CurrentThread.CurrentCulture =
  System.Globalization.CultureInfo.GetCultureInfo("en-US");
date.ToString("d").Dump();
Thread.CurrentThread.CurrentCulture =
  System.Globalization.CultureInfo.GetCultureInfo("en-GB");
date.ToString("d").Dump();

will give the following output:

19.05.2011
19.05.2011
5/19/2011
19/05/2011

And when I change the settings in "Regional and Language settings", customize "short date format" to "yyyy-MM-dd" and restart LinqPad the output is:
2011-05-19
19.05.2011
5/19/2011
19/05/2011

Copyright (c) Marimer LLC