Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Custom DateTime

Tags:

c#

I'm having trouble parsing this datetime:

        DateTime ParseDateTime(string dateString)
        {
            //dateString is "2011-07-22 16:11:14,770"
            var format = "yyyy-MM-dd hh:mm:ss,fff";
            var dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
            return dateTime;
        } 

What am I doing wrong?

like image 715
Robert Avatar asked Jun 26 '26 07:06

Robert


1 Answers

I guess the superfluous @ in the beginning of your format is wrong. So:

var format = "yyyy-MM-dd hh:mm:ss,fff";

You were probably confused by a verbatim string literal which is what @ represents.

For example the following works perfectly fine:

class Program
{
    static void Main()
    {
        var format = "yyyy-MM-dd hh:mm:ss,fff";
        var dateTime = DateTime.ParseExact("2011-07-25 11:10:17,328", format, CultureInfo.InvariantCulture);
        Console.WriteLine(dateTime);
    }
}
like image 132
Darin Dimitrov Avatar answered Jun 28 '26 21:06

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!