• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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