C# 将字符串转换为日期时间

标签 c#

如何在 C# 中将以下字符串转换为 DateTime

Mon Apr 22 07:56:21 +0000 2013

当我尝试使用代码时

Convert.ToDateTime("Mon Apr 22 07:56:21 +0000 2013")

它正在抛出错误

String was not considered as valid DateTime

最佳答案

试试 DateTime.ParseExact

示例:

CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);

更多示例可在 http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx 找到

关于C# 将字符串转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978359/

相关文章:

c# - Protobuf-net r282 在反序列化使用 r249 序列化的对象时出现问题

c# - UWP 网络安装 - “Error in parsing the app package.”

c# - 从 C# 程序集或项目生成类图或方法表的免费工具?

c# - System.Web.Configuration.WebConfigurationManager 和 System.Configuration.ConfigurationManager 之间的行为差​​异

c# - 如何在 C# 中使用 AES 加密然后在 Perl 上解密?

c# - WPF DataGrid 中的虚线水平网格线

c# - 文件存在于 Windows 资源管理器和记事本中,但在我的程序中无法访问

c# - 如何确保两个 .NET 库在功能上相同

c# - Entity Framework 核心 : Fail to update Entity with nested value objects

c# - 从按钮取消异步任务