Date Parsing

Posted on 2010-11-10
Last Modified: 2012-05-10

How do I parse a date string such the following:

May  6 2010  7:18PM
Apr 30 2010 11:27AM

Into a DateTime?

Question by:mike_pro
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 34103674

DateTime d = Convert.ToDateTime("May  6 2010  7:18PM");


DateTime d;

if (DateTime.TryParse("May  6 2010  7:18PM", out d))
    // d holds the converted date
    // string could not be converted to a date object

Open in new window

LVL 16

Expert Comment

ID: 34104013

You can use DateTime.ParseExact method to specify the exact format (like MMM d yyyy h:mmtt) that you are passing in

DateTime dt = DateTime.ParseExact("May  6 2010  7:18PM", "MMM d yyyy h:mmtt", null)
LVL 10

Expert Comment

ID: 34109825
I use

DateTime dt = DateTime.Parse(Value.Trim(), CultureInfo.GetCultureInfo("nl-NL").DateTimeFormat, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AssumeLocal | DateTimeStyles.NoCurrentDateDefault);

Its parses lots of different formats.

Replace the "nl-NL" by your preferred locale ID.

Regards, Jacco

Author Closing Comment

ID: 34147399

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 38
Graphics 2 32
Programmatically storing deadlock graphs with SQL Server 6 52
defining NULL or 0 10 44
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question