问题
i get the string value like 01/05/2012(MM/dd/yyyy) , now i have to convert into datetime
string s="01/05/2012";
datetime dt=convert.todatetime(s);
Now dt value is 1/5/2012, but i have to get dt value 01/05/2012, how to get the value , plese give me any idea about that.
Thanks hemanth
回答1:
You are creating your DateTime
object correctly, you are just looking at the internal representaion of the DateTime. The way that it is output depends on the Format String you give its ToString Method.
i.e.:
class Program
{
static void Main(string[] args)
{
string s = "01/05/2012";
DateTime dt = Convert.ToDateTime(s);
Console.WriteLine(dt.ToString("MM/dd/yyyy"));
Console.ReadLine();
}
}
If you look at this MSDN Page on the DateTime Structure
From above link highlighting mine
DateTime Values and Their String Representations
Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.
Because the appearance of date and time values is dependent on such factors as culture, international standards, application requirements, and personal preference, the DateTime structure offers a great deal of flexibility in formatting date and time values through the overloads of its ToString method. The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern.
来源:https://stackoverflow.com/questions/13466220/how-to-convert-sting-to-datetime-format-like-mm-dd-yyyy