错误:在 C# 中转换为日期格式时,字符串未被识别为有效的日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18567427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error : String was not recognized as a valid DateTime while converting to date format in c#
提问by iJade
Here is the date time format i'm trying to format.I'm getting this date format from twitter apis
这是我正在尝试格式化的日期时间格式。我从 twitter apis 获取此日期格式
string date = "Thu Jul 18 17:39:53 +0000 2013"
i tried
我试过
Convert.ToDateTime(date).ToString("dd/MM/yyyy")
But it says String was not recognized as a valid DateTime.
但它说 String was not recognized as a valid DateTime.
采纳答案by Tim Schmelter
This works:
这有效:
DateTime.ParseExact(dtStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture)
ParseExact
and TryParseExact
allows to use a custom format string. ddd
is the abbreviated day name, MMM
the abbreviated month name, dd
the day number, HH
hours in 24h clock format, mm
minutes, ss
seconds, zzzz
the time-zoneand yyyy
the years.
ParseExact
并TryParseExact
允许使用自定义格式字符串。ddd
是简写的日期名,MMM
缩写的月份名称,dd
天数,HH
在24小时时钟格式,小时mm
分,ss
秒,zzzz
该时区和yyyy
多年。
I have used CultureInfo.InvariantCulture
to specify that the current culture is not used but InvariantCulture
which is similar to "en-US"
.
我曾经CultureInfo.InvariantCulture
指定不使用当前文化,但InvariantCulture
它类似于"en-US"
.
works but after getting date from your line of code i tried to do date.ToString("dd/mm/yyyy") but get the string as 12-12-2013, no slashes
有效,但从您的代码行获取日期后,我尝试执行 date.ToString("dd/mm/yyyy") 但将字符串设为 12-12-2013,没有斜线
/
is a replacement character for your current culture's date-separator which is obviously -
. So also use CultureInfo.InvariantCulture
to specify that the separator should be used without using your current culture:
/
是您当前文化的日期分隔符的替换字符,显然是-
. 所以也用于CultureInfo.InvariantCulture
指定分隔符应该在不使用当前文化的情况下使用:
string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
See: The "/" Custom Format Specifier
请参阅:“/”自定义格式说明符
回答by Karl Anderson
Your date string needs to be this:
您的日期字符串需要是这样的:
Thu Jul 18 2013 17:39:53 +0000
Whatever is producing your string needs to have the year value after the month and day and before the time, like above.
无论产生什么字符串,都需要在月份和日期之后以及时间之前具有年份值,如上所示。
string date = "Thu Jul 18 2013 17:39:53 +0000";
var theDate = Convert.ToDateTime(date);
Note: This will produce a valid .NET DateTime
object.
注意:这将生成一个有效的 .NETDateTime
对象。
UPDATE:
更新:
If you cannot change the string produced, then use the ParseExact
method with a custom format, like this:
如果您无法更改生成的字符串,请使用ParseExact
自定义格式的方法,如下所示:
string date = "Thu Jul 18 17:39:53 +0000 2013";
var theDate = DateTime.ParseExact(date, "ddd MMM dd H:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
回答by Nir Kornfeld
You are trying to convert a non-standard format, so use this:
您正在尝试转换非标准格式,因此请使用以下命令:
string dateStr = "Thu Jul 18 17:39:53 +0000 2013";
DateTime date = DateTime.ParseExact(dateStr, "ddd MMM dd h:mm:ss KKKK yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or build the correct format for your input.
或者为您的输入构建正确的格式。
回答by Soner G?nül
How about like;
喜欢怎么样?
string date = "Thu Jul 18 17:39:53 +0000 2013";
DateTime dt = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
输出将是;
18.07.2013 20:39:53
K
for time zone information in here.
K
时区信息在这里。
Check out for more information;
查看更多信息;
回答by Rohit
Try this
尝试这个
DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)
Its better to use Invariant culture than Current culture
使用不变文化比使用当前文化更好
回答by Ajay
Try using DateTime.ParseExact.
尝试使用 DateTime.ParseExact。
string date = "Thu Jul 18 17:39:53 +0000 2013"
DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
回答by Kashyap Vyas
this.Text="22/11/2009";
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);