Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date Parsing

Posted on 2010-11-10
4
Medium Priority
?
505 Views
Last Modified: 2012-05-10
Hi,

How do I parse a date string such the following:

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

Into a DateTime?

Thanks
0
Comment
Question by:mike_pro
4 Comments
 
LVL 75

Accepted Solution

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

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

or

DateTime d;

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

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
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)
0
 
LVL 10

Expert Comment

by:Jacco
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
0
 

Author Closing Comment

by:mike_pro
ID: 34147399
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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