2

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.

iJade
  • 23,144
  • 56
  • 154
  • 243

7 Answers7

7

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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • dat 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 y?? – iJade Sep 02 '13 at 07:22
2

Try this

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

Its better to use Invariant culture than Current culture

Rohit
  • 10,056
  • 7
  • 50
  • 82
1

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.

Nir Kornfeld
  • 827
  • 7
  • 11
0

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);
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

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;

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Try using DateTime.ParseExact.

  string date = "Thu Jul 18 17:39:53 +0000 2013" 
  DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
Ajay
  • 6,418
  • 18
  • 79
  • 130
0

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

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

Kashyap Vyas
  • 129
  • 1
  • 12