Daniel Cazzulino's Blog : How to parse specific date-time formats using DateTime.ParseExact

How to parse specific date-time formats using DateTime.ParseExact

I’ve done this myself: write an entirely new date-time parser using regular expressions. I’m sure most people implementing standards have done the same (i.e. for syndication libraries, etc.). There’s almost no point in doing so, except for some very specific cases, as DateTime.ParseExact is very flexible through the use of custom DateTime format strings.

The following is an example of how you can parse (and normalize, by removing milliseconds) RFC 3389 date-times:

/// <summary>  
/// Parses and renders <see cref="DateTime"/> instances in a format   
/// compliant with RFC 3389 (see [http://www.ietf.org/rfc/rfc3339.txt)](http://www.ietf.org/rfc/rfc3339.txt%29).  
/// </summary>  
public static class Timestamp  
{  
    const string Rfc3389 = "yyyy'-'MM'-'dd'T'HH':'mm':'ss%K"; 


    public static DateTime Parse(string timestamp)  
    {  
        return DateTime.ParseExact(timestamp, Rfc3389, CultureInfo.CurrentCulture);  
    } 


    public static string ToString(DateTime timestamp)  
    {  
        return timestamp.ToString(Rfc3389);  
    } 


    public static DateTime Normalize(DateTime dateTime)  
    {  
        return Parse(ToString(dateTime));  
    }  
} 

Thanks Juan for reminding me (and causing a full class to be deleted from a project :)).

/kzu

/kzu dev↻d