0

When I tried to convert PDT time to en-US culture it is not working.

 DateTime.Parse("Wed, 08 Jun 2012 12:14:14 PDT",new CultureInfo("en-US"));

But this is working

 DateTime.Parse("Wed, 08 Jun 2012 12:14:14",new CultureInfo("en-US"));
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • It is because the zone abbreviations are not recognised by `Parse`, you would have to either remove them or convert them to relevant offsets (gotta use `ParseExact`) – V4Vendetta May 08 '13 at 07:20
  • Thanks, Is there a way to parse `DateTime` from that string? –  May 08 '13 at 07:22

1 Answers1

1

You can't use time-zones in DateTime.Parse(), but you can handle them separately.

Try this:

var time =  DateTime.Parse("Fri, 08 Jun 2012 12:14:14",new CultureInfo("en-US"));
var zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var pdtTime = TimeZoneInfo.ConvertTimeFromUtc(time, zone);

Update 1: Oops, got this the wrong way around. Here is the (correct) counterpart:

var enUsTime = TimeZoneInfo.ConvertTimeToUtc(pdtTime, zone);

Oh, and by the way - your original example will not work because 08 Jun 2012 is not a Wed, but rather a Fri... ;)

Update 2:

If all you need is a TimeDate, then you can parse is as follows (lin

using System.Linq; // Include a ref to LINQ

(...)

var originalDate = "Fri, 08 Jun 2012 12:14:14 PDT";
// Use LINQ to fetch the first 25 characters (ignoring "PDT"):
var dateToParse = new string(originalDate.Take(25).ToArray());
var result = DateTime.Parse(dateToParse, new CultureInfo("en-US"));
Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • thanks, what i asked is to convert `Wed, 08 Jun 2012 12:14:14 PDT` to `en-US` Culture. But in your `DateTime.Parse` you missed `PDT` –  May 08 '13 at 07:28
  • also can you check your code, `utcNow`? –  May 08 '13 at 07:29
  • I asked for converting from `PDT` to `en-US` but your code is reversed –  May 08 '13 at 07:30
  • Thanks, I will be having only this DateTime String `Fri, 08 Jun 2012 12:14:14 PDT` but not `Fri, 08 Jun 2012 12:14:14`. Can you check your first line of code? –  May 08 '13 at 07:33
  • Ok, I suppose I misunderstood. What do you expect as a result though? If all you want is a `DateTime` with the original date, then can't you just parse the substring, as you did in your second line of code in the original post? – Kjartan May 08 '13 at 07:40
  • I just want output as DateTime. Do i need to replace PDT to "" from DateTime String and then do casting? –  May 08 '13 at 07:43
  • @sss No problem. Added a couple of lines more; what I think might be the easiest/shortest way to parse it. Good luck! :) – Kjartan May 08 '13 at 07:53
  • Thanks that works, i accept your answer –  May 08 '13 at 10:17