1

I'm trying to load datetime and time string in process

DateTime strTime = DateTime.ParseExact(strTime.Trim(), "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture); 

I have several places to process date time string, from dateTimePicker:

 UseDate = dateTimePicker1.Value.ToString("MMMM dd, yyyy - dddd");  

with writing and then loading from the file:

 dateTimePicker3.Value = DateTime.ParseExact(DB, "yyyy-MM-dd", CultureInfo.InvariantCulture);

it works proper with english, but not sure how to prevent if user have different setting like chinese or russian, in this case I got:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid DateTime.

I can prevent input with different punctuation with replace "/", ".", and remove all words from string, but in this case I'm not sure how to figure out proper way.

For example, in debug I see strTime with russian "апреля 10, 2017 - понедельник" string, but how to change it to correct readable date without words. I can't process it with regular expression, because in result I can get something like 102017 or at least 10.20.17 but not 10 April of 2017 which must be 10.04.2017 or 10/04/2017 or 10-04-2017 or even in different order 4/10/2017 etc

3
  • maybe this can help karaszi.com/SQLServer/info_datetime.asp#DtFormatsInput Commented Apr 10, 2017 at 15:46
  • where does your input comes from ? can you make an editmask that only allows one format or does it come from an external source where you cant do anything about it ? Commented Apr 10, 2017 at 15:49
  • @GuidoG Hello, question edited Commented Apr 10, 2017 at 16:02

2 Answers 2

2

You don't have to do any string operation to parse a string to a DateTime, just the correct CultureInfo and format string. Using your example string, the way to parse it would be something like:

string strTime = "апреля 10, 2017 - понедельник";
CultureInfo ci = new CultureInfo("ru-RU");
DateTime dateParsed = DateTime.ParseExact(strTime, "MMMM dd, yyyy - dddd", ci);

As you see, with the appropiate culture and the format string, the date gets parsed correctly.

BTW, is worth noticing that with ParseExact you can have as many format strings as you want, just using string[]:

string[] formats = new string[] 
{
     "MMMM dd, yyyy - dddd", "dd/MM/yyyy", "MM-dd-yyyy"
};
DateTime dateParsed = DateTime.ParseExact(strTime, formats, ci);
Sign up to request clarification or add additional context in comments.

4 Comments

so he has to know which culture info to use. What if its mixed in his input ?
No, you don't need to know it. As OP is using winforms, just using CurrentCulture would make the ParseExact work, as long the date string has the appropiate format
this makes this answer complete +1
@Pikoh Yes this way I can change it to different, but I'm trying to find the way to change any different culture input to Englesh Culture yyyy-M-d
1

You are using DateTime.ParseExact and this means that the format mask that you provide should match exactly what you are passing as string to be converted to a datetime.

In case of a russian conversion of that string ("апреля 10, 2017 - понедельник") you should provide a mask of this type with the appropriate CultureInfo

 "MMMM dd, yyyy - dddd"

Now, if you have different formats to parse you could pass, as second parameter to ParseExact an array of string formats like this one

 string[] formats = new string[] 
 {
    "dd/MM/yyyy hh:mm:ss tt", "MMMM dd, yyyy - dddd"
 };

 CultureInfo ci = new CultureInfo("ru");
 DateTime strTime = DateTime.ParseExact(date, formats, ci, DateTimeStyles.None);

4 Comments

Hello, I'm trying to find solution to change any different culture input to Englesh Culture and yyyy-M-d
But how do you know the input culture if it is not the standard set on the locale of your system? Do you have something that tells you what is the culture of the input string?
But what is not clear from your question is if you have a special application that needs to handle different cultures on the same PC. Usually, if your winforms app runs on a Russian computer then the CultureInfo.CurrentCulture will be set to Russian, while in Italy it will be Italian and so on. A single computer uses the locale settings from the OS unless you have some other needs.
The problem is that you call ToString() when you have a perfect valid date in the Value property. You are converting the date to a string then why do you want to convert it back to a DateTime?. Keep it as a DateTime and only when you need to display it use the ToString() to properly format it. A DateTime has no format by itself, it is the way you want to display it that requires a format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.