• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

DateTime from wide variety of date and time input formats

I'm using C#.  What's the easiest way to parse a string containing date and time, that can be in a variety of formats?  Here are some of the possible inputs:

5/11/2005
5/11/05
05/11/05
05/11/2005
2005-05-11
11-May-05
Wednesday, May 11, 2005
May 11, 2005
Wednesday, 11 May, 2005
11 May, 2005
2005 158/151418 (julian day/hhmmss)

I want to be able to "see" the string like a human would, and automatically convert it to a DateTime.
0
rschaaf
Asked:
rschaaf
1 Solution
 
BurntSkyCommented:
Well, there's a DateTime.Parse() method that will parse some generic date strings, but I can't guarantee it will do everything.  Do you have a way of knowing what format the string will be in?  If so, there's an overload for DateTime.Parse that accepts culture/formatting information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDateTimeClassParseTopic1.asp
0
 
rschaafAuthor Commented:
Nope - I have no idea what the format will be.  That's the problem.
0
 
gregoryyoungCommented:
5/11/2005
5/11/05
05/11/05
05/11/2005
2005-05-11
11-May-05
Wednesday, May 11, 2005
May 11, 2005
above is rather trivial to parse.


2005 158/151418 (julian day/hhmmss)
would be alot of trouble to detect but could be done...

for the above "easy" examples....

replace the month name with the number ...
remove all day names ...
remove all commas ...
replace space or - with /
then DateTime.Parse and all easy ones would work.

Greg
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Bob LearnedCommented:
Are you going to "know" that this is a date, or does it have to be detected as a date, and converted?  

This is not a trivial pursuit :)

My suggestion would be to combine some checks:

(1) Start by assuming a parsable date:  DateTime.Parse.

(2) If that fails, then look to see if it is a Julian Date (regular expression might be good here).

(3) If that fails, say "What the hell do I have here?"

Bob
0
 
b1xml2Commented:
public sealed class DateTimeConverter
{
      private DateTimeConverter() {}
      private static readonly string [] Formats =
            {
                  "d/M/yyyy",
                  "d/M/yy",
                  "dd/MM/yy",
                  "dd/MM/yyyy",
                  "yyyy-MM-dd",
                  "dd-MMM-yy",
                  "dddd, dd MMMM, yyyy"
                  "dd MMMM yyyy"            
            };
      
      public static DateTime ConvertToDate(string value)
      {
            DateTime value;
            try
            {
                  value = DateTime.ParseExact(value,Formats,System.Globalization.CultureInfo.CurrentCulture,System.Globalization.DateTimeStyles.AllowWhiteSpaces);
            }
            catch(Exception e)
            {
                  throw e;
            }
            return value;
            
      }
}
0
 
rschaafAuthor Commented:
b1xml2 - this looks great.  I'm accepting your answer.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now