views:

890

answers:

3

With .NET, I have "Thursday, April 10, 2008 1:30:00 PM" and I want "dddd, dd MMMM, yyyy h:m:s t", "6:09:01 PM" and want ""hh:mm:ss tt", "Fri 29 Aug" and want "ddd d MMM", and so on. It seems I should be able to use DateTimeFormatInfo in some way.

I figured I can format the date with each pattern returned by GetAllDateTimePatterns, and when the original date string and the formated date string match then I have the format. Yet, I want to generate custom formats, not the standard formats.

I want the format string. I do not want the date. I have both the DateTime value and the formatted string value for the date. I need <formatString> as in ToString(<formatString>).

+3  A: 

First of all, do you already have a DateTime type, or do you have a string? If the latter, look at the DateTime.ParseExact() or DateTime.TryParseExact() functions to turn that string into a DateTime.

Once you have the DateTime, it's easy. Just call the DateTime's .ToString() method.

The key to both parts is not DateTimeFormatInfo. Instead, you use a format string. You use the format string with both the [Try]ParseExact() functions and the ToString() function.

Just make sure you know which "culture" you're dealing with.

Joel Coehoorn
This is not answering my question. I want the "format string" that a "date string" represents.
AMissico
@AMissico: You could try some sort of wacky regular expression, I guess, but I agree with Joel: your best bet is to get your source string into a DateTime object.
wweicker
@wweicker - I do have the DateTime object and the formatted value. I thought about regular expressions, but I can do basic soure code manipulation stuff. Nothing advanced. Moreover, I have a feeling that using regular expressions might make the process too complex.
AMissico
I understand now- I misunderstood your question at first. I don't have a good answer for you, but since based on votes it looks like I'm not the only one I'll leave this here so you don't get more wrong answers like this rather than just delete it.
Joel Coehoorn
A: 

If you are looking to convert actual date strings to C# DateTime format strings, this is not possible to do reliably.

How, for example, would you handle this string:

03/04/05 9:00

A few issues with that example:

  1. You do not know is the month, which is the year, etc.
  2. You do not know whether the format string should use 12- or 24-hour clock.
  3. You don't know for certain whether minutes are to be shown accurately, or always replaced with 00s.
RedFilter
Yes, I agree that I cannot do reliably, but if using CultureInfo.DateTimeFormatInfo then it should be fairly reliable. From your example, it would be "MM/DD/yy h:mm", which is close enough, because I don't care if it is 9 am or 2100. That is up to the user.
AMissico
#1 would be resolved with CultureInfo.DateTimeFormatInfo.
AMissico
#2 is data entry issue and the user can fix.
AMissico
#3 is always minutes because we are working with DateTime strings.
AMissico
A: 

Pass the format to the ToString. Using the format you specified:


DateTime d = DateTime.Now;
Console.WriteLine(d.ToString("dddd, dd MMMM, yyyy h:m:s t"));
Console.WriteLine(d.ToString("hh:mm:ss tt"));
Console.WriteLine(d.ToString("ddd d MMM"));


UPDATED to reflect changes in the question.

A given date/time string may match one or more format strings, but you may get closer to what you want to do with something along the following lines:


class FindDateTimeFormat {
    public static void Show() {
        foreach (string item in GetMatchingFormats("Thursday, April 10, 2008 1:30:00 PM")) {
            Console.WriteLine(item);
        }
    }

    private static string[] GetMatchingFormats(string dateTimeString) {
        DateTimeFormatInfo formatInfo = CultureInfo.CurrentCulture.DateTimeFormat;
        List matchingFormats = new List();
        foreach (string format in formatInfo.GetAllDateTimePatterns()) {
            try {
                DateTime dateTime = DateTime.ParseExact(dateTimeString, format, null);
                if (!matchingFormats.Contains(format)) {
                    matchingFormats.Add(format);
                }
            }
            catch (FormatException) {
            }
        }
        return matchingFormats.ToArray();
    }
}

Alfred Myers
Not answering question.
AMissico
Doesn't answer it how? That matches my understanding of your question...
RedFilter
The comment was for the original answer and not the updated answer.
AMissico
Thank you Alfred, but I need the custom formats.
AMissico
You don't want me to do that for you, do you? GetAllDateTimePatterns returns an array that you can combine (add to another list, array) with whatever custom formats you want.
Alfred Myers
No, I don't want you to do that. I am looking for a way to generate the custom format. I do not know what date will be used so I cannot preload the 'matchingFormats' array with custom formats to test against. It would be too difficult to load the array with combinations of custom date and time format strings.
AMissico