Another DateTime.ParseExact question
I have the following code.
using System;
using System.Globalization;
class testCompile
{
static void Main(string[] args)
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "mm/d/yyyy";
string strInput = "11/5/2010";
string strOutput = DateTime.ParseExact(strInput, format, provider).ToString();
Console.WriteLine("string Looks Like : {0}", strOutput);
}
}
1) If I try to set the format to "mm/dd/yyyy", the above code throws an error at runtime.
2) I am getting an output of 1/5/2010 12:11:00 AM for the above code.
Where exactly is the 12:00:00 AM coming from?
How did Guy Fawkes Day change to 5th of Jan?
Co开发者_如何学运维uld someone please explain what is going on?
1) Since you didn't specify a time, it uses the default, 12:00:00 AM.
2) You need to use "MM" to specify the month in your format string, rather than "mm" (minutes). See Custom Date and Time Format Strings for all the details you can handle.
mm
means minutes; MM
means months.
The month should be capital M
or MM
.
See Custom Date and Time Format Strings for the complete reference.
精彩评论