Essential Types C# Book

using System;
class MainClass
{
public static void Main(string[] args)
{
string ds1 = "Sep 2010";
string ds2 = "25 December 2010 13:15:33";
string ds3 = "5,9,5";
string ds4 = "5/9/2010 14:15:33";
string ds5 = "2:15 PM";
DateTime dt1 = DateTime.Parse(ds1);
DateTime dt2 = DateTime.Parse(ds2);
DateTime dt3 = DateTime.Parse(ds3);
DateTime dt4 = DateTime.Parse(ds4);
DateTime dt5 = DateTime.Parse(ds5);
// Display the converted DateTime objects.
Console.WriteLine("String: {0} DateTime: {1}", ds1, dt1);
Console.WriteLine("String: {0} DateTime: {1}", ds2, dt2);
Console.WriteLine("String: {0} DateTime: {1}", ds3, dt3);
Console.WriteLine("String: {0} DateTime: {1}", ds4, dt4);
Console.WriteLine("String: {0} DateTime: {1}", ds5, dt5);
// Parse only strings containing LongTimePattern.
DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
// Parse only strings containing RFC1123Pattern.
DateTime dt7 = DateTime.ParseExact("05 Sep 2010 14:13:30 GMT", "dd MMM yyyy HH':'mm':'ss 'GMT'", null);
// Parse only strings containing MonthDayPattern.
DateTime dt8 = DateTime.ParseExact("September 05", "MMMM dd", null);
// Display the converted DateTime objects.
Console.WriteLine(dt6);
Console.WriteLine(dt7);
Console.WriteLine(dt8);
}
}
The output:
String: Sep 2010 DateTime: 9/1/2010 12:00:00 AM
String: 25 December 2010 13:15:33 DateTime: 12/25/2010 1:15:33 PM
String: 5,9,5 DateTime: 5/9/2005 12:00:00 AM
String: 5/9/2010 14:15:33 DateTime: 5/9/2010 2:15:33 PM
String: 2:15 PM DateTime: 12/25/2010 2:15:00 PM
12/25/2010 2:13:30 PM
9/5/2010 2:13:30 PM
9/5/2010 12:00:00 AM