简体   繁体   中英

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"];
    dtfi.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM