Datetime regional format
-
when i write the date in win.txt it was like this Lsbox_Date.Items.Add(DTP_Today.Value.ToShortDateString()); so if the regional setting was set to MM/dd/yyyy it will write the date the same as regional setting so i change it to this which always gonna be dd/mm/yyyy Lsbox_Date.Items.Add(DTP_Today.Value.Day.ToString()+ "/"+DTP_Today.Value.Month.ToString()+"/"+DTP_Today.Value.Year.ToString()); when i read the win.txt this code is reading the date from win.txt StreamReader Read_key = File.OpenText(@windir+"\\Win.txt"); string key_Trim; string key; key_Trim = Read_key.ReadLine(); key = key_Trim.TrimEnd(); Read_key.Close(); Convert the key to datetime DTP_Key.Value = Convert.ToDateTime(key.ToString()); so if the regional setting is set to dd/mm/yyyy i have no Problem coz the format is the same but if the regional setting is set to mm/dd/yyyy then i get an error converting the key string. i need help pls ASAP
-
when i write the date in win.txt it was like this Lsbox_Date.Items.Add(DTP_Today.Value.ToShortDateString()); so if the regional setting was set to MM/dd/yyyy it will write the date the same as regional setting so i change it to this which always gonna be dd/mm/yyyy Lsbox_Date.Items.Add(DTP_Today.Value.Day.ToString()+ "/"+DTP_Today.Value.Month.ToString()+"/"+DTP_Today.Value.Year.ToString()); when i read the win.txt this code is reading the date from win.txt StreamReader Read_key = File.OpenText(@windir+"\\Win.txt"); string key_Trim; string key; key_Trim = Read_key.ReadLine(); key = key_Trim.TrimEnd(); Read_key.Close(); Convert the key to datetime DTP_Key.Value = Convert.ToDateTime(key.ToString()); so if the regional setting is set to dd/mm/yyyy i have no Problem coz the format is the same but if the regional setting is set to mm/dd/yyyy then i get an error converting the key string. i need help pls ASAP
Use a explicit
CultureInfo
for writing and reading the date to be independent from setting of the PC:System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US"); // use culture of your choice here
DTP_Today.Value.ToString("d", culture); // get short date string in culture-specific formatting
Convert.ToDateTime(key.ToString(), culture); // create from short date string in culture-specific formatting