Archive of: DateTime

  1. Working With Custom Time Strings in C# posted February 12, 2010

    Let’s say we need to process some external data where the format and contents of the files are out of our control. To make matters worse, what if those same external files store tiemstamps in a foreign format that DateTime.Parse() does not understand?

    It would be ill-advised to keep the dates in string format due to the level of robust date processing capabilities present in .NET.

    Luckily, .NET provides a nice way to create custom date formats so any date string can be easily converted into a DateTime object.

    //locale information
    CultureInfo provider = CultureInfo.InvariantCulture;
    
    //The timestamp from the external file
    string dateString = "Dec 07 01:32:25 2009"; 
    
    //the new format
    string format = "MMM dd HH':'mm':'ss yyyy";
    
    //Convert the time string into a legitimate DateTime object
    DateTime result = DateTime.ParseExact(dateString, format, provider);
    

    Now, we have the benefit of leveraging both custom timestamp values and .NET’s built in DateTime Functionality.

    Do you think this might be useful on your current project? Learn more about DateTime.ParseExact() here here.

    Image From: kenyonreview.org
.NET Algorithmic Art ASP ASP .NET ASP .NET MVC audio Bill Gates C# code review Computational Art CouchDB CSS Database DateTime debug DevDays eclipse ethics FileSystem Git Improvement iPhone Java JQuery Knopflerfish Layouts mail Microsoft Oracle OSGi Processing Python Ruby SMTP SOA Software Piracy Source Control SQL StackOverflow tools TortoiseGit training Web Standards Windows XHTML