views:

50

answers:

4

Hi.
I need to parse dates and times from strings. The Problem is that the strings can have any possible format. But I also get the format strings.

So i get:

Date = "9/15/2010"
Time = "16:12:45"
DateFormat = "M/dd/yyyy"
TimeFormat = "h:mm:ss"
TimeZone = "+2:00:00"  // +/- and time in TimeFormat

But i have some problems parsing these strings.

I can't parse the time

DateTime.ParseExact("16:12:45","h:mm:ss",null,DateTimeStyles.None);

does not work and causes a FormatException. What is wrong with this call?

If the DateFormat contains slashes, i need to escape them @"M\/dd\/yyyy". Are there any other chars that would need escaping?

Can i parse the whole DateTime in one? Somehting like:

DateTime.ParseExact(Date+' '+Time+' '+TimeZone,DateFormat+' '+TimeFormat+' +'+TimeFormat,null,DateTimeStyles.None);
A: 

Do you know which culture the original string belongs to? If you do you can specify a culture specific DateTimeStyle when calling Parse()

DateTime date = DateTime.Parse("<your specific date>", System.Globalization.CultureInfo.GetCultureInfo("<your culture>").DateTimeFormat);
Jonas Stensved
No. I don't know the culture. I only get the informations i have written in the top post.
Marks
+2  A: 

What is wrong with this call?

The "h:mm:ss" format string expects the hours element to be in 12-hour format (h); The hours in your string are in 24-hour format so you need to use H instead:

DateTime.ParseExact("16:12:45", "H:mm:ss", null, DateTimeStyles.None);

Are there any other chars that would need escaping?

Any literal character in your string that clashes with a format specifier will need to be escaped. For example, / is the date separator but \/ means the literal / character; : is the time separator but \: means the literal : character; y is one of the year specifiers but \y is the literal y character.

Can i parse the whole DateTime in one?

Yes.

LukeH
Thanks, the strings are coming from a delphi program where h can also be 24 hour format.
Marks
How does this work when the format changes? You have to know the format in advance but your question states that the format can change.
toxaq
A: 

Create a custom DateTimeFormatInfo and pass that into DateTime.Parse. Like this:

string dateValue = string.Format("{0} {1}", "9/15/2010", "16:12:45");
var customDateTimeFormatInfo = new DateTimeFormatInfo();
customDateTimeFormatInfo.FullDateTimePattern = string.Format("{0} {1}", "M/dd/yyyy", "h:mm:ss");
DateTime dt = DateTime.Parse(dateValue, customDateTimeFormatInfo);

No escaping required and handled your "h:mm:ss" fine with no modification required.

toxaq
A: 

How about this?

var @return = (DateTime?)null;
if (source != null)
{
    source = source.Trim();
    if (source.Length > 0)
    {
        var fs = new string[]
        {
            "d MMMM yyyy h:mm tt",
            "d MMMM yyyy h:mm:ss tt",
            "d MMMM yyyy HH:mm",
            "d MMMM yyyy HH:mm:ss",
            "d MMMM yyyy",
            "d/M/yy h:mm tt",
            "d/M/yy h:mm:ss tt",
            "d/M/yy HH:mm",
            "d/M/yy HH:mm:ss",
            "d/M/yy",
            "d/M/yyyy HH:mm",
            "d/M/yyyy HH:mm:ss",
            "d/M/yyyy h:mm:ss tt",
            "d/M/yyyy",
            "d/M/yyyy h:mm tt",
            "d-MMMM-yy HH:mm",
            "d-MMMM-yyyy h:mm tt",
            "d-MMMM-yyyy h:mm:ss tt",
            "d-MMMM-yyyy HH:mm",
            "d-MMMM-yyyy HH:mm:ss",
            "d-MMMM-yyyy",
            "d-MMM-yy",
            "d-MMM-yy h:mm tt",
            "d-MMM-yy h:mm:ss tt",
            "d-MMM-yy HH:mm",
            "d-MMM-yy HH:mm:ss",
            "d-MMM-yyyy",
            "d-M-yy h:mm tt",
            "d-M-yy h:mm:ss tt",
            "d-M-yy HH:mm",
            "d-M-yy HH:mm:ss",
            "d-M-yy",
            "d-M-yyyy",
            "yyyy/M/d h:mm tt",
            "yyyy/M/d h:mm:ss tt",
            "yyyy/M/d HH:mm",
            "yyyy/M/d HH:mm:ss",
            "yyyy/M/d",
            "yyyy-M-d h:mm tt",
            "yyyy-M-d h:mm:ss tt",
            "yyyy-M-d HH:mm",
            "yyyy-M-d HH:mm:ss",
            "yyyy-M-d",
            "yyyy-MM-ddTHH:mm:ss",
        };
        @return = DateTime.ParseExact(source, fs,
            System.Globalization.CultureInfo.CurrentCulture,
            System.Globalization.DateTimeStyles.None);
    }
}
return @return;
Enigmativity