我需要用
SqlDateTime.Parse(val)
其中 val 是一个字符串,例如“23.3.1992 00:00:00 ”。
字符串为欧洲格式,即日在月之前。但是 Parse 需要“美国”格式。我如何告诉它使用特定的日期时间格式/语言环境?
提前致谢!
最佳答案
试试这个:
string val = "23.12.1992 00:00:00";
// Parse exactly from your input string to the native date format.
DateTime dt = DateTime.ParseExact(val, "dd.M.yyyy hh:mm:ss", null);
// Part to SqlDateTime then
System.Data.SqlTypes.SqlDateTime dtSql = System.Data.SqlTypes.SqlDateTime.Parse(dt.ToString("yyyy/MM/dd"));
这可以在一条语句中完成,但只是为了说明而分开。
关于C# 字符串到 SqlDateTime : how to set format for recognition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16814012/