C#将字符串转换为日期时间 [英] C# to Convert String to DateTime

查看:2553
本文介绍了C#将字符串转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下字符串的DateTime 转换成C#?

 周一04月22日7时56分21秒+0000 2013 

当我试图用

  Convert.ToDateTime(周一代码04月22日7时56分21秒+0000 2013)

这是扔误差




字符串未被视为有效的DateTime



解决方案

尝试 DateTime.ParseExact 代替。



  CultureInfo的提供商= CultureInfo.InvariantCulture; 
dateString =太阳报2008年6月15日上午8:30 -06:00;
格式=DDD DD MMM YYYY H:毫米TT ZZZ
结果= DateTime.ParseExact(dateString,格式,供应商);



更​​多的例子可在的 http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx


How to convert the below string to DateTime in C#?

Mon Apr 22 07:56:21 +0000 2013

When i tried the code with

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

it is throwing error as

String was not considered as valid DateTime

解决方案

Try DateTime.ParseExact instead.

Example:

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);

More examples are available at http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

这篇关于C#将字符串转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆