views:

86

answers:

2

I need code that takes in a string and determines the date. I wrote a simple 15line function to return the date assuming it is relative. That was easy. Now i need to take in a string such as "jan 17 09" "1/2/3" (is it MM/DD/YY? or DD/MM/YY, here it means the former). I'll need ambiguous return codes or exceptions. What can i use to parse these absolute dates?

A: 

The date format highly depends on your thread's current culture settings.

leppie
I'll need to pass it in as a parameter from a user setting. But what function can i use of what library?
acidzombie24
You can use `Thread.CurrentUICulture` to get whatever culture the user is running their OS with.
Dan Puzey
Actually i am using IMAP on the server with google apps to detect if the email came from the user or from another party (think of it as writing an email on your mobile phone which updates a status) -edit- oh so i can set with that .NET function and use DateTime.Parse. Sounds good if i can figure out the best culture info for the client.
acidzombie24
A: 

If you know the specific culture that you need to deal with (which you allude to in your comment on Leppie's answer), you can simply use the System.Convert.ToDateTime function to convert your ambiguous string to a specific, absolute date (using the specified culture).

For example:

string s1 = "jan 17 09";
string s2 = "1/2/3";
// Creates a specific culture, irrespective of the "local" culture,
// in this case, Japanese.
CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
DateTime d1 = Convert.ToDateTime(s1, culture);
DateTime d2 = Convert.ToDateTime(s2, culture);
Console.WriteLine(d1.ToLongDateString());
Console.WriteLine(d2.ToLongDateString());
CraigTP