Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a date/time string in specific format

Tags:

c#

I receive some file from external system, the date/time is represented as 28/Jul/2015:01:02:36 -0500.

What is the best way to parse it in to DateTime type in C#?

like image 919
hardywang Avatar asked Dec 02 '25 05:12

hardywang


1 Answers

You should look here for more information on Custom Date Formats in C#:

Custom Date Formats on MSDN

However here is some code to get you started.

First, determine the correct format string you expect. and then use ParseExact

static void Main(string[] args)
{
    var date = "28/Jul/2015:01:02:36 -0500";
    var formatstring = "dd/MMM/yyyy:HH:mm:ss K";

    var d = DateTime.ParseExact(date, formatstring, null);
    Console.WriteLine(d);
    Console.ReadLine();
}

Hope this helps!

like image 80
Glenn Ferrie Avatar answered Dec 03 '25 19:12

Glenn Ferrie