How to create a readable date and time from a string?

I use api, which returns the date as a string, for example:

2011-06-13T21: 15: 19Z

As you can imagine, this is not the easiest format to understand. My goal is to make it format like this:

9:15 pm - 6/13/2011

Does anyone know how to do this? Do I need to use a regular expression or is there a way to convert it to DateTime?

NOTE: I tried to use the method DateTime.ParseExact, but it did not work. If this is a solution, can you show me how to convert the above example. Thank.

+3
source share
6 answers
string date = "2011-06-13T21:15:19Z";
DateTime dt = DateTime.Parse(date);
+3
source

TryParse, . , , , . , , .

        string Time = "2011-06-13T21:15:19Z";

        DateTime t;
        if (DateTime.TryParse(Time, out t)) 
        {
            //Works
        }
+3

DateTime.Parse, , :

var dt = DateTime.Parse("2011-06-13T21:15:19Z");
Console.WriteLine(dt.ToString("h:mmtt - M/d/yyyy"));

, , , ToLower():

Console.WriteLine(dt.ToString("h:mmtt - M/d/yyyy").ToLower());

, :
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

+2

DateTime.Parse ISO 8601.

+1

DateTime.Parse.

void Main()
{
    var date = DateTime.Parse("2011-06-13T21:15:19Z");
    Console.WriteLine(date);
}
+1

, , "hh:mmtt \- M/d/yyyy".

0

All Articles