C# 字符串到 SqlDateTime : how to set format for recognition?

标签 c# asp.net datetime locale sqldatetime

我需要用

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/

相关文章:

c# - 处理从 MySQL 和 C# 返回的数据的好方法是什么?

c# - 有什么方法可以将 .dll 文件转换为 .cs 文件

c# - .NET Core 2 中间件解决方案中的 ClaimsIdentity 问题

python - Python中的自然/相对天数

r - as.Date 返回 NA

c# - 不能在内插字符串周围使用文字大括号

c# - 在 C# 3.0 中初始化字典

c# - 网格线未在 GridView 中显示

asp.net - Powershell 脚本创建一个 "sub"网站

javascript - c# datetime 到带有新日期或数据解析的 javascript 日期