2.7. Create Dates and Times from Strings

Problem

You need to create a System.DateTime or System.DateTimeOffset instance that represents the time and date specified in a string.

Solution

Use the Parse/TryParse or ParseExact/TryParseExact methods of the DateTime or DateTimeOffset structure.

Many subtle issues are associated with using the DateTime and DateTimeOffset structures to represent dates and times in your applications. Although the Parse and ParseExact methods, as well as the TryParse and TryParseExact counterparts, create DateTime or DateTimeOffset objects from strings as described in this recipe, you must be careful how you use the resulting ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.