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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
Excel file not created as expected 7 51
Error in script 11 48
rest webservice call over https via c# 6 28
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now