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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.