How to make DateTime independent of the current culture?

I am a cam trying to convert date and time to a string and vice versa, but doing it so that it works for all cultures.

I basically have a text box (tbDateTime) and a label (lbDateTime). The label tells the user in what format the program is waiting for tbDateTime input. Text field input will be used for the MySQL command.

It currently works as follows:

lbDateTime.Text = "DD.MM.YYYY hh:mm:ss";   // I live in germany
DateTime date = Convert.ToDateTime(tbDateTime.Text);
String filter = date.ToString("yyyy-MM-dd HH:mm:ss");

Now my question is:

  • Is it possible to define a string format for lbDateTime.Text based on the current culture?
  • What format is the Convert.ToDateTime function used in?

I hope you help me. I donโ€™t actually have a computer to test different cultures, so Iโ€™m very afraid that I did something wrong.

+5
3

Convert.ToDateTime DateTime.Parse DateTime.ParseExact. , , .

DateTime.ParseExact , , .

Edit:
Convert.ToDateTime. , : http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx

, System.Threading.Thread.CurrentThread.CurrentCulture.

Edit2:
. DateTime.TryParse DateTime.TryParseExact, , .

Edit3:
... , , . , . , , 01.02.11. , day.month.year month.day.year year.month.day ..

, , , . , ...

. , , , , .

+6

Convert.ToDateTime DateTime.Parse , (CultureInfo.Current).

. :

DateTime data = DateTime.Parse(tbDateTime.Text, new CultureInfo("en-GB"));

DateTime.ParseExact ( DateTime.TryParseExact) . :

DateTime data = DateTime.ParseExact(tbDateTime.Text, "dd'.'MM'.'yyyy HH':'mm':'ss", CultureInfo.InvariantCulture);
+5

:

// Specify the current language (used in the current system you are working on)
CultureInfo currentCulture = CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.ToString());
// Specify the language that we need
CultureInfo myLanguage = CultureInfo.GetCultureInfo("en-US");

// Adapt the DateTime here, we will use the current time for this example
DateTime currentDate = DateTime.Now;
// The date in the format that we need
string myDate = DateTime.Parse(currentDate.ToString(), currentCulture).ToString(myLanguage);
0

All Articles