iJade
iJade

Reputation: 23791

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

Here is the date time format i'm trying to format.I'm getting this date format from 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.

Upvotes: 2

Views: 45279

Answers (7)

Kashyap Vyas
Kashyap Vyas

Reputation: 129

this.Text="22/11/2009";

DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460038

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-zone and yyyy the years.

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

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

/ 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:

string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);

See: The "/" Custom Format Specifier

Upvotes: 7

Ajay
Ajay

Reputation: 6590

Try using DateTime.ParseExact.

  string date = "Thu Jul 18 17:39:53 +0000 2013" 
  DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);

Upvotes: 0

Rohit
Rohit

Reputation: 10236

Try this

  DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)

Its better to use Invariant culture than Current culture

Upvotes: 2

Karl Anderson
Karl Anderson

Reputation: 34846

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.

UPDATE:

If you cannot change the string produced, then use the ParseExact method with a custom format, like this:

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

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98740

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.

Check out for more information;

Upvotes: 0

Nir Kornfeld
Nir Kornfeld

Reputation: 827

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.

Upvotes: 1

Related Questions