C# 根据区域性将字符串转换为日期时间

C# 根据区域性将字符串转换为日期时间,c#,C#,有人知道如何根据用户的文化将字符串转换为日期吗 我正在使用这个代码 DateTime.Parse("2-7-1997", CultureInfo.InvariantCulture); 这很好,因为我的文化是针对我们的 但如果我通过 DateTime.Parse("23-7-1997", CultureInfo.InvariantCulture); 它引发格式异常 字符串未被识别为有效的日期时间 是否有任何东西可以根据用户文化将字符串转换为日期什么定义了“用户文化”??这是一个windows

有人知道如何根据用户的文化将字符串转换为日期吗

我正在使用这个代码

DateTime.Parse("2-7-1997", CultureInfo.InvariantCulture); 
这很好,因为我的文化是针对我们的

但如果我通过

DateTime.Parse("23-7-1997", CultureInfo.InvariantCulture);
它引发格式异常

字符串未被识别为有效的日期时间


是否有任何东西可以根据用户文化将字符串转换为日期

什么定义了“用户文化”??这是一个windows应用程序,一个web应用程序吗???windows应用程序,用户文化意味着文化设置,如日期货币等。请注意此链接您的问题有点混乱,是否要按当前文化解析字符串并输出?如果是windows应用程序,您应该能够转换日期时间,用户的区域性/区域设置应确保将字符串转换为日期。在我的电脑上,您的应用程序将正确转换为2012年7月23日,因为我已选择丹麦语作为我的区域设置。
// convert it to a datetime
// "d" is the required format
var date = DateTime.ParseExact("7/23/1997", "d", CultureInfo.InvariantCulture);

// now you can output the date in the user's culture
var localizedDateString = date.ToString("d");