0

I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".

I tried the following but it gives invalid DateTime exception.

DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));

Is there a way to convert the String exactly to Datetime with UTC offset value?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Robin
  • 283
  • 4
  • 22

2 Answers2

1

To preserve your Offset, use the DateTimeOffset.ParseExact method:

string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
0

I would suggest to try out one of ParseExact methods of DateTime class

mkonvisar
  • 457
  • 4
  • 13