Date Format difference causing crash
Hi I am writing a piece of code to find the latest date among a list of dates, the problem is that the date was specified in a string. I convert it to a DateTime object using:
private DateTime DateRetStr(string ss)
{
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = ConfigurationManager.AppSettings["DateTimeFormat"];
dtf开发者_开发技巧i.DateSeparator = ConfigurationManager.AppSettings["DateTimeSeperator"];
DateTime objDate = Convert.ToDateTime(ss, dtfi);
return objDate;
}
right now when i change my computer i need to change the app.config file to the correct date seperator and format else my program crashes. Is there any way to automatically update the app.config file according to the system format?
Thanks
Why not just use DateTime.Parse(ss)
to get a DateTime object and don't read any configs.
DateTime.Parse
and DateTime.TryParse
methods actually use system settings for date and time representation, so you should be fine without any format strings in your config if you use these methods.
精彩评论