1
votes

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
You're looking for custom formats available beyond DateTime.Now.ToString("ddd mm YYYY") etc?JustLoren
What do you mean parsing a DateTime String into a custom date and Time format string. You want to transform a string representation of a date/time into another? Is the original really a string?Alfred Myers
I update my question to help clarify what I am after.AMissico
Reading the comments to the answers I think I understand now what you are asking for. Given a date in string format such as "Thursday, April 10, 2008 1:30:00 PM", you want a type member that would return "dddd, dd MMMM, yyyy h:m:s t". Is that correct?Alfred Myers
I suggest you update the question because besides not being clear, at least one of your examples is wrong. "Thursday, April 10, 2008 1:30:00 PM" is not equivalent to "dddd, dd MMMM, yyyy h:m:s t". The correct format would be "dddd, MMMM dd, yyyy h:m:s tt". Notice the order of month and date and the AM/PM.Alfred Myers

3 Answers

3
votes

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.

1
votes

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.
1
votes

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();
    }
}