How to convert sting to datetime format like MM/dd/yyyy

爷,独闯天下 提交于 2020-01-20 08:58:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!