0

So tonight I asked a question about storing date time formats in the Application Settings system of .NET. The answer I got makes sense but got me thinking about another situation.

This is only hypothetical, I may not find myself in need of using this but it would be interesting to know what the common answer is.

It seems the Settings system converts a datetime into the "The General Date Long Time ("G")" format (see Standard Date and Time Format Strings on MSDN) which is fine if that is all you want but what if you want to store a value using the "The Round-trip ("O", "o")" format?

As far as I can see at the moment you would need to store that as a string and parse it yourself. Is that the only way when using the designer?

Kioshiki
  • 951
  • 8
  • 22

1 Answers1

-1

The fastest and most efficient way of losslessly serializing a DateTime is using the ToBinary and FromBinary methods.

settings.Timestamp = date.ToBinary();
// ...
DateTime date = DateTime.FromBinary(settings.Timestamp);
Dan
  • 9,717
  • 4
  • 47
  • 65
  • That will probably be the best option in most cases. It's just less human readable in an XML output. One day I might do a quick benchmark to see what difference it really makes using this vs a string formatted value. But for now this will be fine, Thanks. – Kioshiki Aug 12 '12 at 07:56
  • Thanks for the correction. I have amended the answer with a solution that works. – Dan Aug 12 '12 at 17:47