在C#中将字符串转换为DateTime,结尾是EDT [英] convert string to DateTime in C# with EDT at the end

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

问题描述

我必须将这个字符串解析成C#中的datetime对象:

  Wed,13 Apr 2011 07:11: 04 -0400(EDT)

最简单的方法是什么?



我知道有DateTime.Parse和DateTime.ParseExact,但我试图找出上面的自定义格式语法。

解决方案

您需要使用 DateTime。 ParseExact 并传递自定义格式。

类似于:

  var parsed = DateTime .ParseExact(Wed,13 Apr 2011 07:11:04 -0400(EDT),
ddd,dd MMM yyyy HH:mm:ss zzz,null);

注意

不支持时区缩写因为它们没有正式的指定,它们有时是含糊的。

你应该从输入中删除这个来解析上面的内容。如果你知道可能的价值是什么,你可以看看自己解析。


i have to parse this string below into a datetime object in C#:

Wed, 13 Apr 2011 07:11:04 -0400 (EDT)

what is the simplest way of doing this?

I understand there is DateTime.Parse and DateTime.ParseExact but i am trying to figure out what the custom format syntax would be for this above.

解决方案

You need to use DateTime.ParseExact and pass in a custom format.
Something like:

var parsed = DateTime.ParseExact("Wed, 13 Apr 2011 07:11:04 -0400 (EDT)", 
                                 "ddd, dd MMM yyyy HH:mm:ss zzz", null);

Note
Time zone abbreviations are not supported as there is no official designation of them and they are sometimes ambiguous.
You should strip this from the input to parse the above. You could look at parsing that yourself if you know what the possible values will be.

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

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