简体   繁体   中英

string was not recognized as a valid Datetime on Windows Server 2016

i created an app that uses DateTime.ParseExact in the following way:

DateTime.ParseExact(extractedFileDate, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None);

extractedFileDate is storing a string with a date, for example: "Tue Oct 08 12:07:13 2013"

when i run my app from my computer on Windows 10 everything is running smooth, but when i try to run the same app on Windows Server 2016 i get the "string was not recognized as a valid Datetime" error massage.

the content of the date string is showing just the same value.

what can it be ?

david.

A culture has DateTimeInfo which contains specific information about how to parse the string. In example, the names of the days of the week of months of the year.

Your format string contains ddd MMM and your string to parse Tue Oct so this might be the problem when the server has another culture configured.

You are using CultureInfo.CurrentCulture . Try to use your desktop culture when parsing the string.

Update: In your comment you ask how to support multiple cultures. In the case of data transfer or storage the best method is to use CultureInfo.InvariantCulture when (de)serializing DateTime . This culture is independent of the local settings. When presenting to the user (in a GUI) use the default (user's configured) culture.

解:

DateTime.ParseExact(extractedFileDate, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);

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