-1

I get a date in C# from javascript in this format:

"Tue Jan 15 00:00:00 UTC+0530 2008".

How can I convert this to "dd/MMM/yyyy" format?

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
Shailesh
  • 980
  • 8
  • 16
  • `-1` for no research effort, there are so many duplicates: [one](http://stackoverflow.com/q/1877788/944681), [another](http://stackoverflow.com/q/3310530/944681), [one more](http://stackoverflow.com/q/7966559/944681) – Michal Klouda Nov 03 '12 at 09:42
  • Hey Michal, I disagree ... the answer is not obvious. The format is not compatible with DateTime.Parse. I do not see so many duplicates. Perhaps I'm wrong too ... can you give it a closer look and reconsider for Shailesh? – Rob Smyth Nov 03 '12 at 09:51
  • Mr. Michal, I try to many way to do this but i can't get it,if you know the answer plz give me the answer. – Shailesh Nov 03 '12 at 09:57
  • @RobSmyth & Shailesh: I really see no difference from this [question](http://stackoverflow.com/q/1877788/944681), but OK, see my answer.. – Michal Klouda Nov 03 '12 at 10:07
  • Michal - yes your correct that that question is a duplicate. But I did a google for the answer and did not find that stackoverflow answer. In my opinion the -1 is not justified ... unless you still recon that a google will find many answers. Sometimes we just do not add the exact keyword if there are not many answers out there to a specific question. Perhaps your right and I too just missed the obvious. – Rob Smyth Nov 03 '12 at 10:10
  • @Shailesh: If you've tried lots of ways, you should say in the question what you've tried and what happened. Currently there's no *evidence* of research at all. – Jon Skeet Nov 05 '12 at 06:43

2 Answers2

1
 var jsdate = "Tue Jan 15 00:00:00 UTC+0530 2008";
 var format = "ddd MMM d HH:mm:ss UTCzzzzz yyyy";
 var date = DateTime.ParseExact(jsdate, format, CultureInfo.InvariantCulture);

 Console.WriteLine(date.ToString("dd/MMM/yyyy"));
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
1

Try this:

var test = "Tue Jan 15 00:00:00 UTC+0530 2008";
const string format = "ddd MMM d HH:mm:ss zzz yyyy";

var var = DateTime.ParseExact(test.Replace("UTC", ""), format, CultureInfo.InvariantCulture);
Console.WriteLine(var);
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Rob Smyth
  • 1,768
  • 11
  • 19
  • @code4life: If you're suggesting that `var` isn't a valid identifier, you're wrong. It's *not* a keyword in C#; it's a *contextual* keyword - it only has special meaning in certain places. – Jon Skeet Nov 05 '12 at 06:44
  • @JonSkeet: I was more focused on the etymology. Writing `var var` has a bit of code smell, IMHO. – code4life Nov 05 '12 at 15:13
  • @code4life: I'd agree that it's not particularly nice, but your comment wasn't really clear. At least we've clarified it now :) – Jon Skeet Nov 05 '12 at 15:14
  • @JonSkeet: yeah, sometimes my humor is kind of dodgy. Actually, I think it's dodgy on most days, LOL. – code4life Nov 05 '12 at 15:22
  • LOL. I think it is funny too. I did not notice it. Funny and yea it just does not help the readability. – Rob Smyth Nov 06 '12 at 05:48