all groups > dotnet clr > november 2004 >
You're in the

dotnet clr

group:

DateTime.Parse converts a UK format monetary sum into a date


DateTime.Parse converts a UK format monetary sum into a date James Higgs
11/24/2004 4:17:06 AM
dotnet clr:
Hi group,

If you take the following code, the result is not, as one would expect, an
exception, but rather the string "01/12/0500"

DateTime dt = DateTime.Parse("£12,500", new CultureInfo("en-GB"));
Console.WriteLine(dt.ToString("dd/MM/yyyy"));

This also happens with a range of different values, such as £1,500 and £
7,500 (note the space), £12,001 and so on. The string £12,000 causes an
exception to be thrown, correctly.

I've also repro'd this with other cultures: en-US, fr-FR and so on.

Does anyone else have this problem, and is there are workaround or fix? I'm
RE: DateTime.Parse converts a UK format monetary sum into a date kitg NO[at]SPAM ONLINE NO[at]SPAM microsoft.com
11/30/2004 9:30:40 PM
James, we've actually fixed Parse for V2.0, so these kinds of senseless
parses now act more appropriately. Don't however, expect to see the fixes
ported back to V1.1.

You can use ParseExact to give you some better parsing logic:

DateTime dt = DateTime.ParseExact("04-04-2005", "dd/MM/yyyy", new
CultureInfo("en-GB")); // fail
DateTime dt = DateTime.ParseExact("04/04/2005", "dd/MM/yyyy", new
CultureInfo("en-GB")); // pass

ParseExact is a lot stricter, so it can help you catch glaring issues.

Regards,
KIt
kitg@microsoft.com
AddThis Social Bookmark Button