Purpose of DateTime.ParseExact Method
Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly.
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function ParseExact(String, String, IFormatProvider) As DateTime
[C#] public static DateTime ParseExact(string, string, IFormatProvider);
[C++] public: static DateTime ParseExact(String*, String*, IFormatProvider*);
[JScript] public static function ParseExact(String, String, IFormatProvider) : DateTime;
DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", null);
DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.InvariantCulture);
DateTime.Parse("9/1/2009", new CultureInfo("en-US"));
The parameters passed to ParseExact are as follows:
1) The actual date, StartDateTime
2) The format the StartDateTime is CURRENTLY in,
3) The culture (or null)
So what you want to try is this:
//
// Get our date
//
string StartDateTime = "11/24/2006 23:59";
//
// Create a datetime object
//
DateTime myDate = DateTime.ParseExact(StartDateTime,"MM/dd/yyyy H:mm",
null);
//
// Capture our desired format in a string
//
string FormattedDate = myDate.ToString("yyyy-MM-dd HH:mm tt");
//
// Now we can use it as we please
//
Console.WriteLine(FormattedDate);
No comments:
Post a Comment