Parsing strings with a custom pattern into a Date variable

Parsing strings with a custom pattern into a Date variable

The Parse static method of the Date type does a good job in parsing an input string and returning a Date instance. It recognizes and accepts the commonly used separators for the date part, such as / or -, and : for the time. It also has overloaded versions that allow you to specify the culture of the input string, and whether the string can contain leading, trailing or inner white spaces to skip during the parsing. However, at times you may need more flexibility, and the need to parse date strings written in a specific and non-standard format. For example, this is necessary when you’re importing dates from a text file produced by some legacy application that used its own custom format. Say for example that instead of writing the US-standard 10/04/2003 it wrote 031004. Well, another method of the Date type comes to rescue, ParseExact, that allows you to specify the pattern of the input string, for both the date and time parts. Here’s a sample snippet that parses “031004 10:03 AM” into a Date variable:

Dim myDate As Date = Date.ParseExact( "031004 10:03 AM", "yyMMdd hh:mm tt", _    System.Globalization.DateTimeFormatInfo.InvariantInfo)MessageBox.Show(myDate.ToLongDateString() & " - " & myDate.ToLongTimeString())

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several