Gold
Gold

Reputation: 62524

how to ask if any date format fit to another?

how to ask in C# Winform if this format: ddd MMM d HH:mm:ss yyyy == true

then convert to dd/MM/yyyy format

thank's in advance

Upvotes: 1

Views: 87

Answers (1)

Hans Passant
Hans Passant

Reputation: 942040

Use TryParseExact(). Followed by DateTime.ToString() to convert. For example:

    public static string ConvertDate(string arg) {
        DateTime dt;
        if (DateTime.TryParseExact(arg, "ddd MMM d HH:mm:ss yyyy", null, 
              System.Globalization.DateTimeStyles.AssumeLocal, out dt)) {
            return dt.ToString("dd/MM/yyyy");
        }
        // Consider what to return on failure...
        return null;
    }

Test case:

    string s = ConvertDate("Fri Jul 23 10:21:00 2010");

Upvotes: 5

Related Questions