错误:在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:36:46  来源:igfitidea点击:

Error : String was not recognized as a valid DateTime while converting to date format in c#

c#asp.netdatedatetime

提问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)

ParseExactand TryParseExactallows to use a custom format string. dddis the abbreviated day name, MMMthe abbreviated month name, ddthe day number, HHhours in 24h clock format, mmminutes, ssseconds, zzzzthe time-zoneand yyyythe years.

ParseExactTryParseExact允许使用自定义格式字符串ddd是简写的日期名,MMM缩写的月份名称,dd天数,HH在24小时时钟格式,小时mm分,ss秒,zzzz时区yyyy多年。

I have used CultureInfo.InvariantCultureto specify that the current culture is not used but InvariantCulturewhich is similar to "en-US".

我曾经CultureInfo.InvariantCulture指定不使用当前文化,但InvariantCulture它类似于"en-US".

Demo

Demo

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.InvariantCultureto 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 DateTimeobject.

注意:这将生成一个有效的 .NETDateTime对象。

UPDATE:

更新:

If you cannot change the string produced, then use the ParseExactmethod 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

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