1

I have these datetimes which appear to me to be in an odd standard..

2004/05/17 21:27:16.162 GMT-7
2006/08/01 01:00:00 GMT-7
2010/11/05 13:00:38.844 US/Pacific

Any ideas on how I can parse them in C#? Or has anyone seen them before?

Nicholas Mayne
  • 1,694
  • 14
  • 18

2 Answers2

0

Maybe you could take a look at this topic it tells you how to format the datetime

DateTime Localization

But i think what you are looking for is the DateTimeOffset Convertsion between DateTime and DateTimeOffset

Community
  • 1
  • 1
BigL
  • 1,631
  • 1
  • 11
  • 10
0

2010/11/05 13:00:38.844 US/Pacific is something which you cannot parse in pure .Net, at least not in localized form. For others, it really depends on whether you know format. If you do, you can use DateTime's ParseExact() method:

var dateString = "2004/05/17 21:27:16.162 GMT-7";
var anotherDateString = "2006/08/01 01:00:00 GMT-7";
DateTime firstResult;
var success = DateTime.TryParseExact(dateString, "yyyy/MM/dd HH:mm:ss.fff 'GMT'z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out firstResult);
if (success)
    MessageBox.Show(firstResult.ToString());

DateTime anotherResult;
success = DateTime.TryParseExact(anotherDateString, "yyyy/MM/dd HH:mm:ss 'GMT'z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out anotherResult);
if (success)
    MessageBox.Show(anotherResult.ToString());

Please keep in mind, that I used InvariantCulture because I knew it is exactly the same as en-US. For correct results you would need to use valid CultureInfo.

Last, but not least: why you want to parse these invalid formats anyway? I would suggest to fix the source of the problem and use local default formats (depending on the culture as well as local time for current user's location) rather than creating your own. Special formats make understanding time harder as they are unnatural for end users.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79